生成2个独立的随机数 [英] Generating 2 independent random numbers

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

问题描述

生成2个独立随机数的正确方法是什么?

他们将被比较是否相等。


这个方法怎么样:


srand(time(0));

int r1 = rand();

srand(rand());

int r2 = rand();

bool f = r1 == r2;

解决方案

在我的上一篇文章中。张贴我忘了提到

随机数应在0到99之间。

所以这里我更新的问题:


生成

2个0到99范围内的独立随机数的正确方法是什么?

它们将立即进行相等比较。


这个方法怎么样:


srand(time(0));

int r1 = rand()%100;

srand(rand());

int r2 = rand()%100;

bool f = r1 == r2;


bilgekhan写道:


生成2个独立随机数的正确方法是什么?

他们将被比较是否相等。


这种方法怎么样:


srand(time(0));

int r1 = rand();

srand(rand());

int r2 = rand();

bool f = r1 == r2;



如果你真的需要一个随机数源,你最好使用像gcrypt或openssl这样的

加密库a更随机

随机数生成器;他们有一个更加复杂的机制,即为b熵池播种。


----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.4.9(GNU / Linux)

iEYEABECAAYFAkg9PhMACgkQx9p3GYHlUOJLUgCdE0f2M85XqR TCvrSskDagRLbZ

7L0An0BGWYtrtdFvqs2rcSo3TXmlAJ23

= XdOJ

----- END PGP SIGNATURE -----


" bilgekhan" < bi ******* @ bilgekhanbilgekhan.net.trwrites:


在我的上一篇文章中。张贴我忘了提到

随机数应在0到99之间。

所以这里我更新的问题:


生成

2个0到99范围内的独立随机数的正确方法是什么?

它们将立即进行相等性比较。



Sam说的话。


这个方法怎么样:


srand(time(0));

int r1 = rand()%100;

srand(rand());

int r2 = rand()%100;

bool f = r1 == r2;



rand()函数返回0到RAND_MAX之间的伪随机整数。


因此,假设它'' s不包括RAND_MAX,并且包括0和

假设每个值都是等概率的(如果RAND_MAX不是由男人指定的,那么在Linux上的rand(3)页面没有指定) %100不是0,那么你的

randoms r1和r2不会是等概率的。


但这已经假设太多,因为进一步阅读

兰德(3)手册页。

如果你对随机性的关注很少,你也可以写下来


bool f =((rand()%100)==(rand()%100));


-

__Pascal Bourguignon __


What is the correct method for generating 2 independent random numbers?
They will be compared whether they are equal.

What about this method:

srand(time(0));
int r1 = rand();
srand(rand());
int r2 = rand();
bool f = r1 == r2;

解决方案

In my prev. posting I forgot to mention that the
random numbers shall be in the range 0 to 99.
So here my updated question:

What is the correct method for generating
2 independent random numbers in the range 0 to 99 ?
They will immediately be compared for equality.

What about this method:

srand(time(0));
int r1 = rand() % 100;
srand(rand());
int r2 = rand() % 100;
bool f = r1 == r2;


bilgekhan writes:

What is the correct method for generating 2 independent random numbers?
They will be compared whether they are equal.

What about this method:

srand(time(0));
int r1 = rand();
srand(rand());
int r2 = rand();
bool f = r1 == r2;

If you truly need a source of random numbers, you''re better off using a
cryptographic library like gcrypt or openssl that provide a "more random"
random number generator; they have a much more sophisticated mechanism of
seeding the entropy pool.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkg9PhMACgkQx9p3GYHlUOJLUgCdE0f2M85XqR TCvrSskDagRLbZ
7L0An0BGWYtrtdFvqs2rcSo3TXmlAJ23
=XdOJ
-----END PGP SIGNATURE-----


"bilgekhan" <bi*******@bilgekhanbilgekhan.net.trwrites:

In my prev. posting I forgot to mention that the
random numbers shall be in the range 0 to 99.
So here my updated question:

What is the correct method for generating
2 independent random numbers in the range 0 to 99 ?
They will immediately be compared for equality.

What Sam said.

What about this method:

srand(time(0));
int r1 = rand() % 100;
srand(rand());
int r2 = rand() % 100;
bool f = r1 == r2;

The rand() function returns a pseudo-random integer between 0 and RAND_MAX.

Therefore, assuming it''s excluding RAND_MAX, and including 0, and
assuming each value is equiprobable (which is not specified by the man
page of rand(3) on Linux), if RAND_MAX % 100 is not 0, then your
randoms r1 and r2 won''t be equiprobable.

But this is already assuming too much, as further reading of the
rand(3) man page.
If you care so little about your randomness, you could as well write

bool f=((rand()%100)==(rand()%100));

--
__Pascal Bourguignon__


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

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