佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1596|回复: 6

c++ random number 问题

[复制链接]
发表于 24-4-2008 01:23 PM | 显示全部楼层 |阅读模式
这是我的原码

  1. #include<iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main(int argc, char *argv[])
  5. {
  6.  int random[9];
  7.  for (int a = 0;a<9;a++){
  8.   random[a] = (rand()%10)+1;
  9.   cout<<random[a]<<endl;
  10.  }
复制代码
我想问c++每次generate random number 都是一样吗?
为什么每次都execute 同样的四个号码?
我是用 microsoft visual studio 的c++
回复

使用道具 举报


ADVERTISEMENT

发表于 24-4-2008 01:31 PM | 显示全部楼层

回复 1# sushi-x 的帖子

你要用不同的seed.
回复

使用道具 举报

 楼主| 发表于 24-4-2008 01:33 PM | 显示全部楼层
原帖由 KHTAY 于 24-4-2008 01:31 PM 发表
你要用不同的seed.

什么是seed?
回复

使用道具 举报

发表于 24-4-2008 01:37 PM | 显示全部楼层
给你些Info,你可以参考,是英文的。


The sequence of numbers returned by rand() are called random because they satisfy statistical tests for randomness, eg, uniform distribution, coorelation between sequential numbers is zero, no apparent patterns). But of course they really can't be truly random (whatever that means) because computers are deterministic. Therefore they are more properly called pseudorandom numbers.

For a given seed (starting value), the sequence of numbers that rand() returns will always be the same. Because the starting point for the pseudorandom sequence can easily be varied (see below) and because the sequence is very long (perhaps billions before the sequence repeats), these pseudorandom numbers are as good as random.

Having the same sequence generated each time can be useful for debugging, but it isn't very useful when you're actually using the program. To generate a different random sequence, it's necessary to set the seed that starts the sequence. The srand() function takes a positive integer parameter which tells where to start the sequence.

example
int i;
i=srand(2345);


Using the time as a seed - srand(time(0))The standard way to start with a different initial value, the seed, is to use the current time as a seed. Use the time() function as follows:
srand(time(0));  // Initialize random number generator.at the beginning of the program to initialize the random seed. time(0) returns the integer number of seconds from the system clock. This will almost always be a different value.

example:

#include  <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main() {     

  srand((unsigned)time(0));     
  int random_integer = rand();     
  cout << random_integer << endl;

}

[ 本帖最后由 KHTAY 于 24-4-2008 01:52 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 24-4-2008 01:52 PM | 显示全部楼层
可以了,不过是C的
#include<iostream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
        int random[10];
        srand ( time(NULL) );

        for (int a = 0;a<10;a++){
                random[a] = ((rand()%4)+1)*100+((rand()%13)+1);
       
                cout<<random[a]<<endl;
        }
        return 0;
}
回复

使用道具 举报

发表于 24-4-2008 01:54 PM | 显示全部楼层

回复 6# sushi-x 的帖子

哦,那只是给你参考。
回复

使用道具 举报

Follow Us
发表于 24-4-2008 01:55 PM | 显示全部楼层

回复 5# NanTeSD 的帖子

不用羡慕,以后你也会懂的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 28-12-2025 09:13 PM , Processed in 0.144568 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表