Java中的真随机生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/381037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
True random generation in Java
提问by Gareth
I was reading the Math.random() javadoc and saw that random is only psuedorandom.
我正在阅读 Math.random() javadoc 并看到随机只是伪随机。
Is there a library (specifically java) that generates random numbers according to random variables like environmental temperature, CPU temperature/voltage, or anything like that?
是否有一个库(特别是 java)根据环境温度、CPU 温度/电压等随机变量生成随机数?
采纳答案by Greg Dean
Check out http://random.org/
RANDOM.ORG is a true random number service that generates randomness via atmospheric noise.
RANDOM.ORG 是一种真正的随机数服务,它通过大气噪声产生随机性。
The Java library for interfacing with it can be found here: http://sourceforge.net/projects/trng-random-org/
与它交互的 Java 库可以在这里找到:http: //sourceforge.net/projects/trng-random-org/
回答by Joachim Sauer
Since tapping into those sources of random data would require hardware access of some kind such a library can't be written portably using pure Java.
由于利用这些随机数据源需要某种类型的硬件访问,因此无法使用纯 Java 可移植地编写此类库。
You can however try to write platform-dependent code to read the platforms source of random data. For Linux (and possibly other Unix-like systems as well) that could be /dev/random
for example.
但是,您可以尝试编写依赖于平台的代码来读取随机数据的平台源。例如,对于 Linux(也可能是其他类 Unix 系统)/dev/random
。
Also, look at the SecureRandomclass, it might already have what you want.
另外,看看SecureRandom类,它可能已经有了你想要的。
回答by tvanfosson
The Java Cryptographic Architecturerequires cryptographically-strong random numbers. It contains the SecureRandomclass mentioned by @saua.
歼AVA加密架构需要加密的强随机数。它包含@saua 提到的SecureRandom类。
回答by John D. Cook
Be sure that you really want "true" random numbers. Physical sources of randomness have to be measured, and the measurement process introduces some bias. For some applications, "pseudo" random numbers are actually preferable to "true" random numbers. They can have better statistical properties, and you can generate them faster. On the other hand, you can shoot yourself in the footwith pseudorandom number generators if you're not careful.
确保你真的想要“真正的”随机数。必须测量随机性的物理来源,并且测量过程会引入一些偏差。对于某些应用程序,“伪”随机数实际上比“真”随机数更可取。它们可以具有更好的统计属性,并且您可以更快地生成它们。另一方面,如果您不小心,您可能会用伪随机数生成器将自己射中脚。
回答by Josef
There is no truerandom number generator since they all rely one way or another on deterministic procedures to compute a random number, so, no matter how generated numbers appear to follow a true random distribution, they might be a part of a hidden -and very complex- pattern, hence they are Pseudo-Random. However, you can implement your own random number generator, there are a couple of nice, computational-cheap methods you can read in Numerical Recipes in C, Second Edition - Section 7. HTH
没有真正的随机数生成器,因为它们都依赖于确定性程序以一种或另一种方式来计算随机数,因此,无论生成的数字看起来如何遵循真正的随机分布,它们都可能是隐藏的一部分 - 而且非常复杂模式,因此它们是Pseudo-Random。但是,您可以实现自己的随机数生成器,您可以在Numerical Recipes in C, Second Edition - Section 7 中阅读一些不错的、计算成本低的方法。HTH
回答by Josef
In college I had task to implement random generator. I created random number generator like this: created desktop window and asked a user to click on random places on the window, after each click i took coordinates of clicked point. That was pretty random i think.
在大学里,我的任务是实现随机生成器。我创建了这样的随机数生成器:创建桌面窗口并要求用户单击窗口上的随机位置,每次单击后我都会获取单击点的坐标。我认为那是相当随机的。
回答by Ken Gentle
See also this SO question: Alternative Entropy Sources
另见这个问题:Alternative Entropy Sources
I found HotBitsseveral years ago - the numbers are generated from radioactive decay, genuinely random numbers.
几年前我发现了HotBits- 这些数字是由放射性衰变产生的,真正的随机数。
There is a java library for access at randomx
有一个 Java 库可供访问 randomx
There are limits on how many numbers you can download a day, but it has always amused me to use these as really, really random seeds for RNG.
一天可以下载的数字数量是有限制的,但是将这些用作 RNG 的非常非常随机的种子总是让我感到很有趣。
回答by ykaganovich
Your question is ambiguous, which is causing the answers to be all over the place.
你的问题模棱两可,导致答案到处都是。
If you are looking for a Random implementation which relies on the system's source of randomness (as I'm guessing you are), then javax.crypto.SecureRandom does that. The default configuration for the Sun security provider in your java.security file has the following:
如果您正在寻找依赖于系统随机源的 Random 实现(我猜您是这样),那么 javax.crypto.SecureRandom 会这样做。java.security 文件中 Sun 安全提供程序的默认配置如下:
#
# Select the source of seed data for SecureRandom. By default an
# attempt is made to use the entropy gathering device specified by
# the securerandom.source property. If an exception occurs when
# accessing the URL then the traditional system/thread activity
# algorithm is used.
#
# On Solaris and Linux systems, if file:/dev/urandom is specified and it
# exists, a special SecureRandom implementation is activated by default.
# This "NativePRNG" reads random bytes directly from /dev/urandom.
#
# On Windows systems, the URLs file:/dev/random and file:/dev/urandom
# enables use of the Microsoft CryptoAPI seed functionality.
#
securerandom.source=file:/dev/urandom
If you are really asking about overriding this with something even more truly random, it can be done either by changing this property, or by using another SecureRandom. For example, you could use a JCE provider backed by an HSM module such as nCipher nShieldwhich has its own PRNG, or other solutions mentioned in the thread.
如果您真的想用更真正随机的东西覆盖它,可以通过更改此属性或使用另一个 SecureRandom 来完成。例如,您可以使用由 HSM 模块(如nCipher nShield)支持的 JCE 提供程序,它有自己的 PRNG,或线程中提到的其他解决方案。
回答by tuinstoel
Wikipedia quote: John von Neumann famously said "Anyone who uses arithmetic methods to produce random numbers is in a state of sin."
维基百科引述:约翰·冯·诺依曼(John von Neumann)有句名言:“任何使用算术方法产生随机数的人都处于犯罪状态。”
回答by Guemundur Bjarni
For most purposes, pseudo-random numbers are more than enough. If you just need a simple random number, ie. in 30% of the time do this, then a timestamp as a seed is what you want. If this has to be secure random number, for example shuffling a deck, you want to choose your seed a bit more carefully, there are good sources out there for creating secure seeds.
对于大多数用途,伪随机数已绰绰有余。如果您只需要一个简单的随机数,即。在 30% 的时间里这样做,那么作为种子的时间戳就是你想要的。如果这必须是安全的随机数,例如洗牌,您需要更仔细地选择您的种子,那里有创建安全种子的好资源。
The reason for using seeds is to be able to "recall" the same sequence of random numbers generated by the algorithm. A very good scenario for that is when you are doing stochastic simulation on some sort and you want to repeat a particular experiment, then you simply use the same seed.
使用种子的原因是能够“召回”算法生成的相同随机数序列。一个非常好的场景是当您对某种类型进行随机模拟并且想要重复特定实验时,您只需使用相同的种子。
For a better PRNG than the one bundled with Java, take a look at the Mersenne Twister.
要获得比与 Java 捆绑的 PRNG 更好的 PRNG,请查看Mersenne Twister。