关于兰德() [英] About rand()
问题描述
标准说rand()应该返回一个伪随机的
数,但伪随机是什么意思?如果一个实现
的rand()总是返回相同的数字它是否符合要求?
如果它总是在两个值之间交替怎么办?
在实际方面,你对rand()的行为实际可以期待什么?b $ b?可以
例如一个人预计最终会返回范围内的任何值
[0,RAND_MAX]吗?
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?
On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?
推荐答案
Spiros Bousbouras写道:
Spiros Bousbouras wrote:
标准说rand()应该返回一个伪随机的
数字,但伪随机是什么意思?如果一个实现
的rand()总是返回相同的数字它是否符合要求?
如果它总是在两个值之间交替怎么办?
在实际方面,你对rand()的行为实际可以期待什么?b $ b?可能
例如一个人预计最终会返回范围内的任何价值
[0,RAND_MAX]?
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?
On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?
这完全是一个QoI问题。实际上,写一个
rand()
是微不足道的,它会在某个时刻返回每个值,即使分布是
不完美。 [我已经看到的一些实现复制了K& R2中显示的示例
。]
常见问题解答中有很多关于rand()。
任何需要在一个严肃的程序中使用随机数的人都会有可能从过多的PRNG中获得自己的例行程序
净额。 [例如。 http://www.stanford.edu/~ blp / writings / clc / random.html。]
-
彼得
It''s entirely a QoI issue. Pragmatically, it is trivial to write a
rand()
that returns every value at some point, even if the distribution is
not perfect. [A few implementations I''ve seen copy the example
shown in K&R2.]
The FAQ has a number of comments on the issues of rand().
Anyone needing to use random numbers in a serious program will
likely roll their own routine from the plethora of PRNG''s available on
the net. [E.g. http://www.stanford.edu/~blp/writings/clc/random.html.]
--
Peter
Peter Nilsson写道:
Peter Nilsson wrote:
Spiros Bousbouras写道:
Spiros Bousbouras wrote:
标准说那个rand()应该返回一个伪随机的
数,但伪随机是什么意思?如果一个实现
的rand()总是返回相同的数字它是否符合要求?
如果它总是在两个值之间交替怎么办?
在实际方面,你对rand()的行为实际可以期待什么?b $ b?可能
例如一个人预计最终会返回范围内的任何价值
[0,RAND_MAX]?
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?
On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?
这完全是一个QoI问题。实际上,写一个
rand()
是微不足道的,它会在某个时刻返回每个值,即使分布是
不完美。 [我已经看到的一些实现复制了K& R2中显示的示例
。]
It''s entirely a QoI issue. Pragmatically, it is trivial to write a
rand()
that returns every value at some point, even if the distribution is
not perfect. [A few implementations I''ve seen copy the example
shown in K&R2.]
完美你的意思是均匀分布吗?
By "perfect" distribution do you mean uniform distribution ?
文章< 11 ******************* ***@v45g2000cwv.googlegroups .com> ;,
Spiros Bousbouras< sp **** @ gmail.comwrote:
In article <11**********************@v45g2000cwv.googlegroups .com>,
Spiros Bousbouras <sp****@gmail.comwrote:
> ;标准说rand()应该返回伪随机数,但伪随机是什么意思?如果rand()的实现总是返回相同的数字,那么它是否符合要求?
如果它总是在两个值之间交替怎么办?
>The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?
它必须产生一个伪随机序列,范围为0
到RAND_MAX,其中RAND_MAX至少为32767.如果在特定的情况下
实现它永远不会产生RAND_MAX然后RAND_MAX
该实现将被定义为它可以产生的最大价值,并且如果该值不是至少32767那么
实施将是不符合的。
可以类似地争辩说,如果0不能产生那个
实施不符合要求;这个论点可能是
a有点弱。
如果实现在0和RAND_MAX之间交替,那么
就会符合要求。
It must produce a pseudo-random sequence in the range 0
to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
implementation it can never produce RAND_MAX then RAND_MAX
for that implementation would be defined as the largest value that
it -could- produce, and if that value was not at least 32767 then
the implementation would be non-conforming.
One could similarily argue that if 0 cannot be produced that
the implementation is non-conforming; the argument is perhaps
a bit weaker.
If the implementation alternated between 0 and RAND_MAX then
it would be conforming.
>在实际方面,你对兰德()的行为实际上可以期待什么有什么想法?可能
例如,人们期望最终将返回范围
[0,RAND_MAX]中的任何值?
>On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?
我不会 - 期望 - 任何自尊的rand()不能
能够产生该范围内的一个值,最终;
我会 - 在最坏的情况下 -
标准中给出的样本函数。但是,如果某个组织产生类似C语言的组织使用了他们想要的东西 -
是一个很好的兰特()但结果证明这并不会让我感到震惊。不能产生一些价值的
。 [注意:
生成类C语言的组织数量远远超过那些产生符合C语言的组织数量。]
-
如果你撒谎到编译器,它将报复。 - Henry Spencer
I would not -expect- any self-respecting rand() to not be
able to produce one of the values in the range, eventually;
I would -expect- at worst the sample function given in the
standard. But it wouldn''t shock me if some organization
that produced a C-like language used what they -thought-
was a good rand() but which turned out not to be able to produce
some set of values. [NB: the number of organizations that
produce C-like languages appears to far outnumber the ones that
produce conforming C.]
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
这篇关于关于兰德()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!