Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

True random generation in Java

Tags:

java

random

I was reading the Math.random() javadoc and saw that random is only psuedorandom.

Is there a library (specifically java) that generates random numbers according to random variables like environmental temperature, CPU temperature/voltage, or anything like that?

like image 512
Gareth Avatar asked Dec 19 '08 14:12

Gareth


People also ask

Is Java random truly random?

Random is not completely random at all. It generates sequences in a perfectly predictable way from the seed. You are completely correct that, since the seed is only 64 bits long, it can only generate 2^64 different sequences.

What is random generator in Java?

Random Number Generation with JavaRandom class is used to generate random numbers of different data types such as boolean, int, long, float, and double. An object of Random class is initialized and the method nextInt(), nextDouble() or nextLong() is used to generate random number. You can also use Math.

How do you generate a random number from 1 to 10 in Java?

For example, to generate a random number between 1 and 10, we can do it like below. ThreadLocalRandom random = ThreadLocalRandom. current(); int rand = random. nextInt(1, 11);


2 Answers

Check out http://random.org/

RANDOM.ORG is a true random number service that generates randomness via atmospheric noise.

The Java library for interfacing with it can be found here: http://sourceforge.net/projects/trng-random-org/

like image 84
Greg Dean Avatar answered Oct 06 '22 18:10

Greg Dean


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 java.security.SecureRandom does that. The default configuration for the Sun security provider in your java.security file has the following:

# # 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 nShield which has its own PRNG, or other solutions mentioned in the thread.

like image 41
ykaganovich Avatar answered Oct 06 '22 19:10

ykaganovich