Gre*_*ean 27
RANDOM.ORG是一种真正的随机数服务,可通过大气噪声产生随机性.
可以在此处找到与其连接的Java库:http: //sourceforge.net/projects/trng-random-org/
yka*_*ich 21
你的问题含糊不清,导致答案到处都是.
如果您正在寻找依赖于系统随机性源的随机实现(正如我猜测的那样),那么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
Run Code Online (Sandbox Code Playgroud)
如果你真的要求用更真实随机的东西覆盖它,可以通过更改此属性或使用其他SecureRandom来完成.例如,您可以使用由HSM模块支持的JCE提供程序,例如nCipher nShield,它具有自己的PRNG或线程中提到的其他解决方案.
由于利用这些随机数据源需要某种类型的硬件访问,因此使用纯Java无法编写这样的库.
但是,您可以尝试编写与平台相关的代码来读取随机数据的平台源.对于Linux(也可能是其他类似Unix的系统),/dev/random例如.
另外,看看SecureRandom类,它可能已经拥有了你想要的东西.
又快又脏:
public static int generateRandom() throws IOException
{
int num = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0 ; i < Integer.SIZE ; i++)
{
System.out
.println("Flip a fair coin. Enter h for heads, anything else for tails.");
if (br.readLine().charAt(0) == 'h')
{
num += Math.pow(2, i);
}
}
return num;
}
Run Code Online (Sandbox Code Playgroud)