随机不够随机? [英] random is not random enough?
问题描述
我使用随机生成随机数,因此:
int x,y;
for(y = 0; y < 5; y ++)
{
x = random(50);
cout<< x;
}
运行此程序时,会生成5个随机数。但是,如果我停止程序并重新运行它,则会生成相同的序列。我不会忘记做某事吗?还有另一个随机数生成器
我可以试试吗?
I am using random to generate random numbers, thus:
int x,y;
for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}
When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?
推荐答案
JNY写道:
我使用随机生成随机数,因此:
int x,y;
for(y = 0; y< 5; y ++)
{
x = random(50);
cout<< x;
}
运行此程序时,会生成5个随机数。但是,如果我停止程序并重新运行它,则会产生相同的序列。我忘了做某事吗?还有另一个随机数发生器
我可以试试吗?
I am using random to generate random numbers, thus:
int x,y;
for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}
When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?
你忘了给发电机播种。关于''srand''函数的RTFM。
V
You are forgetting to seed the generator. RTFM on ''srand'' function.
V
JNY写道:
JNY wrote:
我使用随机生成随机数,因此:
int x,y;
for(y = 0; y< 5; y ++)
{
x =随机(50);
cout<< x;
}
运行此程序时,会生成5个随机数。但是,如果我停止程序并重新运行它,则会产生相同的序列。我忘了做某事吗?还有其他随机数生成器我可以试试吗?
I am using random to generate random numbers, thus:
int x,y;
for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}
When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?
你遗漏了srand函数。检查你的文档或谷歌搜索详细信息。
Your missing the srand function. check you docs or google it for details.
" JNY" < JN ** @ hotmail.com>在消息中写道
news:91 *********************** @ posting.google.com。 ..
"JNY" <jn**@hotmail.com> wrote in message
news:91***********************@posting.google.com. ..
我使用随机生成随机数,因此:
int x,y;
for(y = 0; y< ; 5; y ++)
{
x =随机(50);
cout<< x;
}
运行此程序时,会生成5个随机数。但是,如果我停止程序并重新运行它,则会产生相同的序列。我忘了做某事吗?
是的。种子随机发生器有一些数字(例如,自从
EPOCH以来的秒数)。
见 http://www.manpage.org/cgi-bin/man/man2html?query=srand
还有其他随机数发生器,我可以试试吗?
I am using random to generate random numbers, thus:
int x,y;
for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}
When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something?
Yes. Seed the random generator with some number (the number of seconds since
EPOCH, for instance).
See http://www.manpage.org/cgi-bin/man/man2html?query=srand
Is there another random number generator which I could try?
很多。
Many.
这篇关于随机不够随机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!