True random generation in Java

Tags: random java
By : Gareth
Source: Stackoverflow.com
Question!

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?

By : Gareth


Answers

Just to clarify: The only TRUE random generator that exist in the universe is Quantum Random Bit Generator. There is no other mechanism that will assure you, that generated bits are totally random, because even if now you cannot predict the result there is no guarantee that you won't be able to to that in the future.

Quantum Random Bit Generator' (QRBG121), which is a fast non-deterministic random bit (number) generator whose randomness relies on intrinsic randomness of the quantum physical process of photonic emission in semiconductors and subsequent detection by photoelectric effect. In this process photons are detected at random, one by one independently of each other. Timing information of detected photons is used to generate random binary digits - bits. The unique feature of this method is that it uses only one photon detector to produce both zeros and ones which results in a very small bias and high immunity to components variation and aging. Furthermore, detection of individual photons is made by a photomultiplier (PMT). Compared to solid state photon detectors the PMT's have drastically superior signal to noise performance and much lower probability of appearing of afterpulses which could be a source of unwanted correlations.

More information for example here: http://random.irb.hr/

By : Marek


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;
}


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.



This video can help you solving your question :)
By: admin