The random class is used in Java for the resulting stochastic number, new Random (10): 10 is the number of seeds. Note: One of the characteristics of random is that random objects of the same seed number are
exactly the same as those generated by the
same number of times . Verification Code:random r1 = new Random (ten); random r2 = new Random (ten); for (int i = 0;i < 4;i++) { System.out.println (R1.nextint (5)); }System.out.println ("++++++++++++++++++++++");for (int i = 0;i < 4;i++) { System.out.println (R2.nextint (5)); }results: Random numbers produced by R13030++++++++++++++++++++++3 random number generated by R2030Replace with:System.out.println (r1.nextdouble (5)) System.out.println (r2.nextdouble (5) )Results:0.73043029674342720.25780279059578040.0592019658112445950.24411725056425315++++++++++++++++++++++0.73043029674342720.25780279059578040.0592019658112445950.24411725056425315 Analysis: Although it is a random number generator, but still in accordance with an algorithm step-by-step implementation, the number of seeds a certain algorithm, the same time the resulting value of course the same!!
* @param seed the initial seed
* @see #setSeed (Long)
*/
The construction method of ++++++++++++++++++ with seed number +++++++++++++
Public Random (long Seed) {
if (getclass () = = Random.class)
This.seed = new Atomiclong (initialscramble (Seed));
else {
Subclass might have overriden Setseed
This.seed = new Atomiclong ();
Setseed (seed);
}
}
++++++++++++++Netint method The source code with parameters ++++++++++++
* @since 1.2
*/
public int Nextint (int n) {
if (n <= 0)
throw new IllegalArgumentException ("N must be Positive");
if ((N &-N) = = n)//i.e., n is a power of 2
return (int) ((n * (long) Next) >> 31);
int bits, Val;
do {
bits = Next (31);
val = bits% n;
} while (Bits-val + (n-1) < 0);
return Val;
}
It is visible that the seed requirement for random is greater than 0 ...
+++++++++++++++nextdouble method implementation +++++++++++
Public double nextdouble () {
Return (((long) (next) << + next (27))
/(Double) (1L << 53);
}
+++++++++++++++nextfloat method implementation +++++++++++++
public float nextfloat () {
Return next ((float) (1 << 24));
}
+++++++++++++++++nextint method implementation: ++++++++++
public int Nextint () {
Return next (32);
}
It can be seen that all random number generation is related to a method called Next , and this method is:
*/
protected int next (int bits) {
long oldseed, Nextseed;
atomiclong seed = this.seed;
do {
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareandset (Oldseed, Nextseed));
return (int) (Nextseed >>> (48-bits));
}
General computer random number is pseudo-random number, with a true random number (seed) as the initial conditions, and then with a certain algorithm iterative generation of random numbers, the following two methods:
algorithm 1: square takes the middle method.
1) Set the seed to X0, and MoD 10000 to get 4 digits
2) to get a 8-digit square (less than 8 bits in front of the 0)
3) Take the middle 4-digit number to get the next 4-bit random number X1
4) Repeat 1-3 steps to generate multiple random numbers
One of the main drawbacks of this algorithm is that it eventually degrades to 0 and cannot continue to generate random numbers.
algorithm 2: linear with congruential
1) Set the seed to X0,
2) with an algorithm x (n+1) = (a*x (n) +b) mod C generates X (N+1)
C is generally made very large, can produce a pseudo-random number between 0 to C-1
One drawback of this algorithm is that there is a loop.
expansion :
There is also an random method in the math class that works by generating a random fraction of a [0,1.0] interval.
By reading the source code of the math class, you can see that the random method in the math class is directly implemented by invoking the Nextdouble method in the random class.
* @see random#nextdouble ()
*/
public static double random () {
Random rnd = RandomNumberGenerator;
if (rnd = = null) rnd = INITRNG ();
return rnd.nextdouble ();
}
Reference : http://www.cnblogs.com/Coffee-guy/p/3378776.html
Http://blog.sina.com.cn/s/blog_4b3120470100k96z.html
Random (Long Seed) method in Java with the use of the Rrandom () method