简体   繁体   中英

Generate True Random Number Generator in any of programming language

As per documentation, java.util.Random is not a true random generator.

From below link: http://docs.oracle.com/javase/7/docs/api/java/util/Random.html , it has been quoted that


An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula.


I have to implement True Random Number Generator. Please suggest alternatives in any programming language Or in java

You are trying to generate a random number using only a deterministic alghorithm , expresed in some programming language like java. It is not possible to create a true random number generator in a deterministic device.

By the very definition of deterministic we know that if a device is configured in the same state it will always exhibit the same behaviour.

In the particular case of a true random generator the device would be an algorithm with no input and one output (the dessired random number). Under such conditions a deterministic algorithm can only produce always the same number. Thus not random.

Pseudorandom generators imitate randomness by storing an internal state they use to produce a sequence of numbers with an appearance of randomness. Conceptually that internal state is just another input of the algorithm and for the same internal state the algorithm will always produce the same output. Though these algorithms provide a stream of changing numbers they are not at all truly random because they will always produce the same string of numbers.

You can try to improve a bit more by initializing the internal state of such pseudorandom algorithm with a different value each time, thus getting different stream of random numbers. But this poses two problems :

  • Where do you get that initial state from? If you always use the same initial state you don't get different streams of numbers. And trying to algorithmically (thus deterministically) get a random state is just the same problem again.
  • Even if you get a true random initial state from a physical source the stream of numbers will still not be truly random. Each number is calculated by a deterministic algorithm from the previous state. Thus by examining a long stream of numbers it is possible to eventually know what will the next number be.

It is actually possible to create a random number generator using an algorithm. You would have to use a nondeterministic algorithm. But such algorithms require a random number generator themselves in order to be nondeterministic. Such as physical devices or race conditions.

It's impossible to generate a true random number generator in code really. It has to be a physical source of randomness and even then there are some questions over how random is random enough to be called true random.

You can buy various USB random number generators that you can plug in and get a value from in your application.

As @depperm commented, this is close to impossible with current computers. It's also an interesting philosophy question, whether the notion of "truly random" is even meaningful...

But if you want something "more random" than a pseudo-random number generator, you can get some kind of "real world" events involved. Don't ask the user for a random seed -- people are not very good at being consciously "random". Some things you can try (any of these can then be used to "seed" a pseudo-random number generator):

  • Have the user move the mouse around randomly, and hash up their movements;
  • Check thing like the lower few bits of the current time (like the milliseconds or microseconds) -- though depending on the system, they could always be 0, or always a multiple of 16, or something).
  • Get a number from someplace like https://www.random.org (they use atmospheric noise)

-s

Many modern computers have hardware means of generating true random numbers. Microprocessors often have an instruction that uses a reversed-biased diode or some thermal noise amplifier to generate random bits and whiten them, and then pass them to the OS. There are also dongles you can buy that plug into a USB port and generate random numbers for you.

Even when the system does not have specific RNG hardware, OSs can gather entropy from keyboard and mouse timings and provide numbers based on those. On Linux and OSX, you can use /dev/random to access this. On Windows, there's CryptGenRandom(). A well-implemented Linux will use the processor's native RNG as well, but some may not.

Apart from the OS, there are web services like random.org, which produces random numbers from weather data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM