rand()在几次试验后选择相同的数字 [英] rand() chooses the same number after several trials

查看:68
本文介绍了rand()在几次试验后选择相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用标准rand()函数生成几个

随机数。即使我在循环之前播种生成器

" srand((unsigned)time(NULL)); ,它通常会在过程中选择一个先前的选定数字,经过6-7次迭代,我希望

序列是唯一的。我应该考虑进一步修改

代码实现我的目标可能吗?这是一段代码..


vector< intrands;

int rnd; //随机数

srand((无符号)时间(NULL));

int range_max = 165,range_min = 0;

for( int i = 0; i< 15; i ++)

{

rnd =(double)rand()/(RAND_MAX + 1)*(range_max-1 -range_min )+

range_min;

rands.push_back(rnd);

}


通常选择7-8

迭代后的先前选择的随机数,通常不是所有时间..


问候,

Hello, i m using the standard rand() function to generate several
random numbers. Even if i seed the generator before the loop
"srand( (unsigned)time( NULL ) );" , it usually selects a previously
selected number in the process, after 6-7 iterations, i want the
sequence to be unique..should i consider some further modifications in
the code to achieve my goal maybe ? here is the piece of code..

vector<intrands ;
int rnd ; //random number
srand( (unsigned)time( NULL ) );
int range_max = 165, range_min = 0 ;
for( int i = 0; i < 15 ; i++ )
{
rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
range_min;
rands.push_back(rnd);
}

it usually chooses a previously selected random number after 7-8
iterations, not all the time but usually..

Regards,

推荐答案

8月29日,11:50,kkirtac< kadir.kir ... @ gmail.comwrote:
On 29 Aug., 11:50, kkirtac <kadir.kir...@gmail.comwrote:

您好,我使用标准rand()函数生成几个

随机数。即使我在循环之前播种生成器

" srand((unsigned)time(NULL)); ,它通常会在过程中选择一个先前的选定数字,经过6-7次迭代,我希望

序列是唯一的。我应该考虑进一步修改

代码实现我的目标可能吗?这是一段代码..


vector< intrands;

int rnd; //随机数

srand((无符号)时间(NULL));

int range_max = 165,range_min = 0;

for( int i = 0; i< 15; i ++)

{

rnd =(double)rand()/(RAND_MAX + 1)*(range_max-1 -range_min )+

range_min;

rands.push_back(rnd);


}


它通常在7-8

迭代后选择一个先前选择的随机数,而不是所有时间但通常..


问候,
Hello, i m using the standard rand() function to generate several
random numbers. Even if i seed the generator before the loop
"srand( (unsigned)time( NULL ) );" , it usually selects a previously
selected number in the process, after 6-7 iterations, i want the
sequence to be unique..should i consider some further modifications in
the code to achieve my goal maybe ? here is the piece of code..

vector<intrands ;
int rnd ; //random number
srand( (unsigned)time( NULL ) );
int range_max = 165, range_min = 0 ;
for( int i = 0; i < 15 ; i++ )
{
rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
range_min;
rands.push_back(rnd);

}

it usually chooses a previously selected random number after 7-8
iterations, not all the time but usually..

Regards,



nomatter你做的事情总是随机的psudo。通过使用复杂的

数学和东西你可以看起来它不是,但确实如此。还有

其他选项。我听说有人试图通过静态和类似的声卡来帮助生成随机数字,但是

真的我不认为这是一个上帝的选择。另外还有一些设备,你可以插入你的计算机,生成真正的随机数。

我最喜欢的是: http://random.irb.hr/

但它依赖于你有互联网连接。

nomatter what you do it will allways be psudo random. By using complex
math and stuff you can it seem like it isnt, but it is. there are
other options though. Ive heard of people experimenting with geneating
random numbers by the help of the soundcard static and simular, but
really i dont think that is a god option. Allso there are devices that
you can plug into your computer which generates truly random number.
My favorite so far is: http://random.irb.hr/
however it relys on you having got a internet conenction.


" kkirtac" < ka ********** @ gmail.com写信息

新闻:11 ******************** **@o80g2000hse.googlegr oups.com ...
"kkirtac" <ka**********@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...

您好,我使用标准rand()函数生成几个

随机数字。即使我在循环之前播种生成器

" srand((unsigned)time(NULL)); ,它通常会在过程中选择一个先前的选定数字,经过6-7次迭代,我希望

序列是唯一的。我应该考虑进一步修改

代码实现我的目标可能吗?
Hello, i m using the standard rand() function to generate several
random numbers. Even if i seed the generator before the loop
"srand( (unsigned)time( NULL ) );" , it usually selects a previously
selected number in the process, after 6-7 iterations, i want the
sequence to be unique..should i consider some further modifications in
the code to achieve my goal maybe ?



是... rand()生成伪随机数,但它不保证那些

数字是唯一的。 />
考虑生成0到1之间的随机数(即只有0或1),

如果我得到0然后是0然后0那么它就不会那么随机那么1

(另外)

你需要检查一下你没有把生成的数字推到''rands''

之前;如果是这种情况你应该生成另一个随机数和

祈祷它会有所不同:)

或者你可以在网上搜索更好的洗牌算法。

Yes... rand() generate pseudorandom numbers, but it doesn''t guarantee those
numbers to be unique.
Consider when generating a random number between 0 and 1 (i.e. only 0 or 1),
it wouldn''t be so random then if I got a 0 then 1 then 0 then 1
(alternatively)
You need to check that you didn''t push the generated number in ''rands''
before; if that''s the case you should generate another random number and
pray it will be different :)
Or you could search the internet for a better "shuffling" algorithm.


rnd =(double)rand()/(RAND_MAX + 1)*(range_max-1 -range_min)+

range_min ;
rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
range_min;



为什么不使用:

rnd = range_min + rand()%rang_max;

代替?我认为它更清晰,你通过省略额外的乘法来获得一些性能,不知道为什么有些人使用你正在使用的风格

虽然。


希望有所帮助,

-

Abdo Haji-Ali

程序员

In | Framez

Why not use:
rnd = range_min + rand() % rang_max;
instead? I think it''s much clearer and you gain some performance by omitting
the extra multiplication, don''t know why some use the style you''re using
though.

Hope that helps,
--
Abdo Haji-Ali
Programmer
In|Framez


Abdo Haji-Ali写道:
Abdo Haji-Ali wrote:

" kkirtac" ; < ka ********** @ gmail.comwrote in message
"kkirtac" <ka**********@gmail.comwrote in message

> rnd =(double)rand()/(RAND_MAX + 1)*(range_max-1 -range_min)+
range_min;
> rnd = (double)rand() / (RAND_MAX + 1) * (range_max-1 -range_min) +
range_min;



为什么不使用:

rnd = range_min + rand()%rang_max;

代替?我认为它更清晰,你通过省略额外的乘法来获得一些性能,不知道为什么有些人使用你正在使用的风格

虽然。

Why not use:
rnd = range_min + rand() % rang_max;
instead? I think it''s much clearer and you gain some performance by omitting
the extra multiplication, don''t know why some use the style you''re using
though.



因为PRNG的较低位通常比较高的

位少得多。请参阅comp.lang.c常见问题解答,问题13.16:
http: //c-faq.com/lib/randrange.html


在这种情况下,OP的代码质量更好。


-

Philip Potter pgp< atdoc.ic.ac.uk

Because the lower bits of a PRNG are often much less random than the higher
bits. See the comp.lang.c FAQ, question 13.16:
http://c-faq.com/lib/randrange.html

The OP''s code is better quality under such conditions.

--
Philip Potter pgp <atdoc.ic.ac.uk


这篇关于rand()在几次试验后选择相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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