We often say that random numbers in Java are pseudo-random numbers, so we need to know what pseudo-random numbers are.
What is a pseudo-random number?
1, the real meaning of the random number in a certain generation process is in accordance with the distribution of the probability of the occurrence of random, the results are unpredictable, is not visible. This is really random.
2, the computer random function is based on the determination algorithm simulation, the result is determined, is visible, we can think of this predictable result of its occurrence probability is 100%, so called pseudo-random.
Java random number generation principle :
The random number generation of Java uses a linear congruence formula, which means that it is generated by a complex algorithm. If interested, you can go to the Java random number of source code parsing blog. http://www.codes51.com/article/detail_1581162_5.html
There are two ways in which Java generates random numbers:
1, Math.random ()
2. New Random ()
java.lang.Math.Random:
The Math.random () function can return a double value between 0-1, evenly distributed within the range (approximate).
Java.util.Random:
There are two ways to construct Random ():
1. Random (): The number of milliseconds in the current time can be obtained by System.currenttimemillis () using the number of milliseconds of the current time as a seed. Here is the source code for the JDK.
Public Random () {This (System.currenttimemillis ());}.
2. Random (long Seed): pass in a seed directly, if you create two Random instances with the same seed, make the same method call sequence for each instance, and they will generate and return the same sequence of numbers.
Nextint () of the Random object, nextint (int n) Method:
int Nextint ()
Returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator.
int nextint (int n)
Returns a pseudo-random number, which is an int value that is taken out of the sequence of this random number generator and evenly distributed between [0,1].
Java generates random numbers within a specified range