How does the Java random number generator work?

I wrote a program that simulates a die roll

Random r = new Random(); int result = r.nextInt(6); System.out.println(result); 

I want to know if there is a way to β€œpredict” the next generated number and how does the JVM determine which number is for the next?

Will my code output real close random numbers in any JVM and OS?

+7
source share
4 answers

They are pseudo-random numbers, which means that for general purposes and goals they are quite random. However, they are determined and completely dependent on the seed. The following code will print the same 10 numbers twice.

 Random rnd = new Random(1234); for(int i = 0;i < 10; i++) System.out.println(rnd.nextInt(100)); rnd = new Random(1234); for(int i = 0;i < 10; i++) System.out.println(rnd.nextInt(100)); 

If you can select a seed, you can pre-calculate the numbers, and then reset the generator with the same seed, and you will know in advance which numbers come out.

+14
source

I want to know if there is a way to β€œpredict” the next generated number and how does the JVM determine which number to generate next?

Absolutely. The Random class is implemented as a linear congruent number generator (LCNG). The general formula for a linear congruent generator is:

 new_state = (old_state * C1 + C2) modulo N 

The exact algorithm used by Random is specified in javadocs . If you know the current state of generator 1, the next state is completely predictable.

Will my code output be close to real random in any JVM and OS?

If you use Random , then no. Not for any JVM on any OS.

The sequence generated by the LCNG is definitely not random and has statistical properties that differ significantly from the true random sequence. (The sequence will be strictly automatically correlated, and it will be displayed if you display the results of consecutive calls in Random.nextInt() .)

This is problem? Well, it depends on what your application needs. If you need "random" numbers that are difficult to predict (for example, for a security-related algorithm), then obviously not. And if numbers will be used for Monte Carlo simulations, then automatic LCNG correlation can distort the simulations. But if you just build a solitaire card game ... maybe that doesn't matter.


1 - For clarity, the state of a Random object consists of the values ​​of its instance variables; see source code You can check them using the debugger. As a last resort, you can access them and even update them using Java reflection, but I would not recommend doing this. The "previous" state is not recorded.

+11
source

Yes, you can predict which number the random number generator will generate. I saw that this is called cracking, hacking, or attacking the RNG. Searching for any of these terms together with a "random number generator" should lead to great results.

Read How We Learned to Cheat Online Poker: A Software Security Survey for Excellent First-Hand Evaluation of How a Random Number Generator Can Be Attacked. To summarize, the authors found that RNG is used based on the erroneous shuffling algorithm used by the online poker site. They then found out the seeds of RNG, taking samples that were distributed. Once they had the algorithm and the seed, they knew exactly how the deck would be organized after later shuffling.

You can also link to the link.

+1
source

Check How java.util.Random works and how good is it? :

In other words, we start from some beginning or "seed" number, which ideally is "really unpredictable," and in practice it is "unpredictable." For example, the number of milliseconds, or even nanoseconds, since the computer was turned on for most systems. Then, every time we want a random number, we multiply the current seed by some fixed number, a, add another fixed number, c, then take the result modulo another fixed number, m. The number a is generally large. This method of generating random numbers returns pretty much until dawn. Almost every β€œrandom” random number generator that you can think of, from scientific calculators to 1980's home computers to the current C and Visual Basic library functions, uses some version of the above formula to generate its random numbers.

As well as Predicting the Next Math.random () in Java

+1
source

Source: https://habr.com/ru/post/1243212/


All Articles