25

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?

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
Gareth
  • 2,180
  • 5
  • 19
  • 24

13 Answers13

29

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
  • 29,221
  • 14
  • 67
  • 78
  • 26
    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 Dec 19 '08 at 14:23
  • 3
    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 Dec 19 '08 at 14:26
  • 3
    Is atmospheric noise truly random or are there detectable patterns in it? – Pop Catalin Dec 22 '08 at 19:30
  • 3
    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 Dec 28 '08 at 02:23
  • @PopCatalin there are obvious patterns and correlations in such raw measurements. That's why they apply a 'whitening phase'. (Randomness Extractor on wikipedia) – Mark Jeronimus May 06 '15 at 12:12
24

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.

ykaganovich
  • 14,736
  • 8
  • 59
  • 96
9

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
  • 10,934
  • 3
  • 34
  • 37
8

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
  • 302,674
  • 57
  • 556
  • 614
  • 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 Dec 19 '08 at 15:02
7

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
  • 29,517
  • 10
  • 67
  • 94
  • 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 Dec 19 '08 at 14:49
  • That sounds like an algorithm, sorta like a pseudorandom number generator. :-) – John D. Cook Dec 22 '08 at 03:26
  • 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 Dec 22 '08 at 14:33
4

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

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
3

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/

Marek
  • 3,935
  • 10
  • 46
  • 70
  • Came across this by accident. You don't need the photomultipliers to get quantum indeterminacy. See [this](http://www.reallyreallyrandom.com/zener/why-its-random/) about the humble Zener diode. Similarly, you get the same degree of true randomness in a digital camera sensor. That's the noise on your party pics. The _" Quantum Random Bit Generator"_ is just marketing hype from certain hardware manufacturers. – Paul Uszak Oct 29 '21 at 12:02
3

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
  • 353
  • 1
  • 2
  • 8
  • Umm, how about connecting a Geiger counter to the computer? That's true random, isn't it? – Tamas Czinege Dec 22 '08 at 20:44
  • 1
    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 Dec 22 '08 at 23:27
2

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
  • 4,082
  • 1
  • 18
  • 14
1

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.

  • 3
    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 Dec 19 '08 at 17:15
  • I think it matters a lot what do you do with the raw data. I think combining a few sources with low predictability and doing weird things with them should be good enough. AFAIK what /dev/random does. Even without a true RNG it has historically worked pretty well. Although slow. On the other hand combined with a prng it has worked historically well with /dev/urandom and much faster. – akostadinov Feb 25 '13 at 14:44
  • There must be some randomness, even holding down the "F" key. For example, I bet you don't know how many nanoseconds was it held down for? – ban-geoengineering Sep 10 '14 at 09:44
1

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.

Community
  • 1
  • 1
Ken Gentle
  • 13,277
  • 2
  • 41
  • 49
1

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

tuinstoel
  • 7,248
  • 27
  • 27
0

As far as i know they work with time of the machine ... !

What Random Numbers Are Used For

Random numbers have been used for many thousands of years. Whether it’s flipping a coin or rolling a dice, the goal is to leave the end result up to random chance. Random number generators in a computer are similar — they’re an attempt to achieve an unpredictable, random result.

Is this possible to make true random numbers ?

Yes it is !

To generate a “true” random number, the computer measures some type of physical phenomenon that takes place outside of the computer.

For a more day-to-day example, the computer could rely on atmospheric noise or simply use the exact time you press keys on your keyboard as a source of unpredictable data, or entropy. For example, your computer might notice that you pressed a key at exactly 0.23423523 seconds after 2 p.m.. Grab enough of the specific times associated with these key presses and you’ll have a source of entropy you can use to generate a “true” random number.

The NSA and Intel’s Hardware Random Number Generator

To make things easier for developers and help generate secure random numbers, Intel chips include a hardware-based random number generator known as RdRand. This chip uses an entropy source on the processor and provides random numbers to software when the software requests them.

source : HowToGeek ?

Just in case that you maybe need to generate random numbers in Android... I use Data of Accelerometer for true physic based random numbers :)

Disco4uf
  • 147
  • 1
  • 12