views:

2044

answers:

12

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?

+13  A: 

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/

Greg Dean
Maybe I'm paranoid, but if I were using it for anything security related, I wouldn't trust a random number supplied by a third party. Better to go with a platform solution -- especially since one is available.
tvanfosson
There is no platform solution that will generate *True* random numbers. But you're right, if you need randomness for security, use the Crypto API.
Greg Dean
Is atmospheric noise truly random or are there detectable patterns in it?
Pop Catalin
A JCA provider can implement SecureRandom with a hardware RNG, which are increasingly common as part of TPMs and some Intel chipsets. These use unpredictable quantum mechanical phenomena to (slowly) produce truly random bits. Since the rate is low, it's best to use this as a seed for a good PRNG.
erickson
+6  A: 

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.

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.

Also, look at the SecureRandom class, it might already have what you want.

Joachim Sauer
Hardware can be accessed via USB and via TCP/IP. I believe the first can be written portably in pure Java, and I know for a fact the second can.
skiphoppy
+3  A: 

The Java Cryptographic Architecture requires cryptographically-strong random numbers. It contains the SecureRandom class mentioned by @saua.

tvanfosson
+5  A: 

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 foot with pseudorandom number generators if you're not careful.

John D. Cook
any good physical random number generator uses the measurements as a starting point, and runs a lot of bit-shuffling to nullify any bias.
Javier
That sounds like an algorithm, sorta like a pseudorandom number generator. :-)
John D. Cook
right, but with a physical seed, and checking how many bits of physical entropy it gets so it doesn't outputs more bits than that.
Javier
+1  A: 

There is no true random 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

Josef
Umm, how about connecting a Geiger counter to the computer? That's true random, isn't it?
DrJokepu
Any natural chaotic system (including radiation) I think is a valid source of true randomization, the problem with Pseudo random generators is it's unclear whether or not it can be mathematically described using some interpolation technique for example.
Josef
A: 

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.

they have actually conducted the "monkeys with typewriters" experiment, which is similar to what you describe. IIRC the findings were that usually the monkeys just held down the "F" key (or some other convenient key). User behavior is hardly random.
Jimmy
That doesn't sound very random to me..
dancavallaro
+1  A: 

See also this SO question: Alternative Entropy Sources

I found HotBits several years ago - the numbers are generated from radioactive decay, genuinely random numbers.

There is a java library for access at 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.

Ken Gentle
+5  A: 

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:

#
# 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.

ykaganovich
A: 

Wikipedia quote: John von Neumann famously said "Anyone who uses arithmetic methods to produce random numbers is in a state of sin."

tuinstoel
A: 

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.

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.

Guðmundur Bjarni
A: 

Quick and dirty:

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;
}
Adam Jaskiewicz
A: 

tvanfosson: You are right, but if you were quite paranoid, you shouldn't trust on your computer PRNG either (it may be compromised, bugged, predicatable etc.) . An intermediate solution maybe XOR both: the result of the local PRNG and the result of the third-party supposed random numbers. This XOR operation is supported by the Java implementation.

SSL/TLS is supported too, though it is not a perfect solution for cryptography applications, because one needs to initialize the SSL connection by using pseudo-random numbers generated locally.