随机数生成器 [英] random number geneator

查看:86
本文介绍了随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:

我正在尝试编写一个简单的程序,模拟向他们的出生日询问几个人

,并计算有多少人被要求直到两个人有

同一天出生。我遇到的问题是第一个循环我得到一个

随机数序列,我得到一个匹配,然后在下面的

循环我得到了随机数SAME (?) 序列。我正在使用rand()。我不想使用随机数生成器过于花哨,但有没有办法让每次启动循环时停止rand()重置?
还有什么其他

我有选择吗?我可以以某种方式使用系统时间来帮助我随机获得更多

吗?下面是到目前为止我的代码:


的#include<&的iostream GT;

的#include< cstdlib>


使用命名空间std;


int main()

{

int const tests = 10000;

int index;

int i,j;

int match = 0;

int days [366] = {0};

int count = 0;


for(j = 1; j< = tests; j ++)

{

while(match!= 1)

{

index =(rand()%365)+ 1;


if(days [index]!= 1)

{

days [index] ++;

count ++;

}

其他

{

for(i = 1; i< 366; i ++)

{

天[i] = 0;

}


match = 1;

}


}


cout<<计数<<结束;

}


返回0;

}

解决方案

在comp.lang.c ++

" Sonoman" <所以***** @ aol.com>写道:

使用随机数生成器过于花哨,但有没有办法在每次启动循环时停止rand()重置?我有什么其他的选择?我可以以某种方式使用系统时间来帮助我获得更多
随机吗?




是的,添加这样的一个电话:


srand(时间(NULL));


和你完成的工作。



-----原始消息-----

来自:" Sonoman" < so ***** @ aol.com>

新闻组:comp.lang.c ++

发送时间:2004年1月7日星期三下午6:25

主题:随机数geneator

大家好:
我想编写一个简单的程序,模拟询问几个
他们的出生日和人数是多少,直到两个人在同一个出生日有
。我遇到的问题是第一个循环我得到一个随机数序列,我得到一个匹配,然后在下面的循环我得到SAME随机(?)序列。我正在使用rand()。我不希望
对随机数生成器太过花哨,但有没有办法在每次启动循环时停止rand()重置?我有什么其他的选择?我可以以某种方式使用系统时间来帮助我获得更多随机吗?到目前为止,这是我的代码:

#include< iostream>
#include< cstdlib>


添加标题,< ctime>
使用命名空间std;

int main()
{
int const tests = 10000;
int index;
int i,j;
int match = 0;
int days [366] = {0};
int count = 0;

for(j = 1; j< = tests; j ++)
{
while(match!= 1)


在循环开始时添加,

" srand(clock());"

{
index =( rand()%365)+ 1;

if(days [index]!= 1)
{天/ [index] ++;
count ++;
}

{
for(i = 1; i< 366; i ++)
{
days [i] = 0; }

匹配= 1;
}

}
这里你需要放一些东西来改变种子。在

" srand(clock());"声明,例如

需要的计数器< Enter>每10个循环按一次

if(counter%10 == 9)cin.get();
cout<<计数<< endl;
}


我想你想要最后一个"}"上面的括号cout<<计数<< endl;"

返回0;
}




请记住,这些仍然是伪随机数,但至少

种子将通过按< Enter>来改变。




" Pierre Espenan" < WH ************** @ yahoo.com>在消息中写道

新闻:zi ******************* @ newsread1.news.pas.eart hlink.net ...



[SNIP]
为(J = 1; J< =测试; j ++)
{while / match!= 1)



在循环开始时添加,
" srand(clock( ));




这个建议是一个关于随机性的坏主意。应该在一个程序中将srand称为

一次(!!)并且永远不要在循环内部。原因是

调用srand一次会产生一个起始种子,它定义了产生随机数的序列。在调用rand()后,内部种子会更新

,这样下一个调用或rand()将导致下一个随机数

这个特定的序列。如果你更频繁地调用srand(),你将会从序列跳到序列,这肯定会破坏随机性。虽然

对于这种情况可能不是一个真正的问题你应该避免多次调用srand,除非你确切知道你在做什么和什么

结果是。


问候

克里斯


Hi all:
I am trying to write a simple program that simulates asking several persons
their birth day and it counts how many persons are asked until two have the
same birth day. The problem that I have is that the first loop I get a
sequence of random numbers untuil I get a match, BUT then on the following
loops I get the SAME random(?) sequence. I am using rand(). I do not want to
get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random? Here is my code so far:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int const tests = 10000;
int index;
int i,j;
int match = 0;
int days[366] = {0};
int count = 0;

for (j = 1; j <= tests; j++)
{
while (match != 1)
{
index = (rand() % 365) + 1;

if (days[index] != 1)
{
days[index]++;
count++;
}
else
{
for (i = 1; i < 366; i++)
{
days[i] = 0;
}

match = 1;
}

}

cout << count << endl;
}

return 0;
}

解决方案

In comp.lang.c++
"Sonoman" <so*****@aol.com> wrote:

get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random?



Yes, add ONE call like this:

srand(time(NULL));

and your done.



----- Original Message -----
From: "Sonoman" <so*****@aol.com>
Newsgroups: comp.lang.c++
Sent: Wednesday, January 07, 2004 6:25 PM
Subject: random number geneator

Hi all:
I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I have is that the first loop I get a
sequence of random numbers untuil I get a match, BUT then on the following
loops I get the SAME random(?) sequence. I am using rand(). I do not want to get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random? Here is my code so far:

#include <iostream>
#include <cstdlib>
add the header, <ctime>

using namespace std;

int main()
{
int const tests = 10000;
int index;
int i,j;
int match = 0;
int days[366] = {0};
int count = 0;

for (j = 1; j <= tests; j++)
{
while (match != 1)
at the begining of the loop add,
"srand(clock());"
{
index = (rand() % 365) + 1;

if (days[index] != 1)
{
days[index]++;
count++;
}
else
{
for (i = 1; i < 366; i++)
{
days[i] = 0;
}

match = 1;
}

} Here you need to put something to change the "seed" in the
"srand(clock());" statement, for example a counter that
requires <Enter> to be pressed every 10 loops
if(counter%10==9) cin.get();
cout << count << endl;
}
I think you wanted the last "}" bracket above "cout << count << endl;"
return 0;
}



Keep in mind that these are still pseudorandom numbers, but at least the
seed will be changed by pressing the <Enter>.



"Pierre Espenan" <wh**************@yahoo.com> wrote in message
news:zi*******************@newsread1.news.pas.eart hlink.net...


[SNIP]


for (j = 1; j <= tests; j++)
{
while (match != 1)



at the begining of the loop add,
"srand(clock());"



This advice is a bad, bad idea regarding randomness. srand should be called
only once(!!) in a program and NEVER inside a loop. The reason is that
calling srand once will yield in a start seed that defines the sequence of
random numbers produced. After calling rand() the internal seed is updated
so that the next call or rand() will result in the next random number of
this specific sequence. If you call srand() more often you are going to jump
from sequence to sequence which will certainly destroy randomness. Although
for this case it might not be a real problem you should refrain from
multiple calls to srand, unless you know exactly what you are doing and what
the results are.

Regards
Chris


这篇关于随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆