且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

rand()真的那么糟糕吗?

更新时间:2023-10-06 13:03:16

问题中的测试并未真正测试随机性.它所做的只是确保数字均匀分布.这是必要条件,但不是充分条件:还有许多其他方法可以使随机数生成器不足.

The test in your question doesn't really test for randomness. All it does is ensure that the numbers are uniformly distributed. This is a necessary but not a sufficient condition: there are many other ways in which a random number generator can be deficient.

例如,如果我为您提供了一个循环返回数字0、1、2,...,999的函数,则该函数也将通过您的测试.然而,它显然将无法对随机性做出任何合理的定义.

For example, if I gave your a function that returned the numbers 0, 1, 2, ..., 999 in a loop, it would also pass your test. Yet it would clearly fail any reasonable definition of randomness.

要查看实践中如何测试随机数生成器,请查看

To see how random number generators are tested in practice, take a look at

  • http://csrc.nist.gov/groups/ST/toolkit/rng/documents/nissc-paper.pdf
  • http://www.random.org/analysis/
  • http://www.random.org/analysis/Analysis2005.pdf

要专门讨论rand(),请查看 rand()被认为有害.

For a discussion of rand() specifically, check out rand() Considered Harmful.