您的位置:首页 > 编程语言 > Java开发

《Java编程技巧1001条》354条:了解伪随机与真随机

2017-12-19 13:02 405 查看
《Java编程技巧1001条》354条:了解伪随机与真随机

354Understanding Pseudo Random Versus True Random

354 弄清伪随机数与真随机数 

When Java, or most other computer languages,needs to produce a random number, a special calculation is made based on agiven input value called a seed number. The calculation will always produce thesame number when the same seed number is given. Because the
functions result isalways predictable, it is called a pseudo-random number. A true-random numberis not predictable. The fact that the pseudo-random number is predictable doesnot mean that it cannot make a good random number. The seed number in Java is64 bits
long and the algorithm which uses it to create a - 11 - random numberproduces a good distribution of values. A big advantage of pseudo-randomnumbers is that your program is easier to debug when it uses the same sequenceof random numbers. After you release
your software to users, you can ensuregood randomness by using a different seed for each program run. For example,the seed could be based on the time, user keystrokes, network activity, or anyother random phenomena. 当 Java,或其它计算机语言, 需要产生一随机数时, 就要利用一个称作种子(数)的已知输
入数来进行一种特殊的计算. 当给出的是同一种子时, 则计算的结果也永远是相同的一个数. 这是因为,函数的计算总是确定的, 其结果也就总是可以预测的. 所以,这样的随机数叫伪随机 数. 真随机数则应该是不确定的, 不可预测的. 伪随机数的可预测性并不意味它不能造就出一个好的随机数. Java 的种子数是 64 位的长 整数,它用来产生随机数的算法能产生数值分布很好的随机数. 伪随机数的一个很大优点在于它可使你的程序容易调试,因为它可以使用相同序列的一 串数. 当你把你的软件交给用户使用后,你可以让每一次运行使用不同的种子以保证产生很好
的随机性. 例如,你可以根据当时的时间,用户的击键次数,网络的作业,或其它随机现象来计算 种子的数值. 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: