pseudo-Random (preundorandom): Random numbers generated by the algorithm are pseudo-random!!
only random numbers generated by real random events are really random!! For example, random numbers are generated through the machine's hardware noise, and random numbers are generated through atmospheric noise .
Random numbers produced by Random are pseudo-random numbers !!!
is a pseudo-random number produced by a deterministic function (common linear congruence), through a seed (a common clock). This means: If you know the seed, or the random number that has already been generated, you may get information about the sequence of random numbers (predictability)
The random class has two constructor methods for implementing the stochastic number generator:
Random () |
Construct a random number generator |
Random (Long Seed) |
Construct a random number generator with seed seeds |
I. Non-parametric construction method (seed not set)
Although on the surface we do not set the seed, but the random construction method has its own seed generation mechanism, the source code is as follows:
1 /**2 * Creates a new random number generator. This constructor sets3 * The seed of the random number generator to a value very likely4 * To is distinct from any and invocation of this constructor.5 */6 PublicRandom () {7 This(seeduniquifier () ^ system.nanotime ());8 }9 Ten Private Static LongSeeduniquifier () { One //L ' Ecuyer, "Tables of Linear congruential generators of A //Different Sizes and good Lattice Structure ", 1999 - for (;;) { - LongCurrent =seeduniquifier.get (); the LongNext = current * 181783497276652981L; - if(Seeduniquifier.compareandset (current, next)) - returnNext; - } + } - + Private Static FinalAtomiclong Seeduniquifier A=NewAtomiclong (8682522807148012L);
Generate seed Process:(reference decryption random number generator (ii)--see linear congruence algorithm from Java source code)
1. Get a long shape number as "initial seed" (system default is 8682522807148012L)
2, constantly multiplied with a perverted number--181783497276652981l (days know that these numbers are not engineers casually roll out of the keyboard-.-) to get an unpredictable value, until you can put the value of this can not be expected beforehand Static constant Seeduniquifier assigned to the Random object. Because the assignment operation may fail in a multithreaded environment, the for (;;) To make sure the assignment succeeds.
3, with the system random out of the Nanotime value for the difference or operation, to obtain the final seed
Nanotime is a more random parameter that describes the execution time of the code. Description of Nanotime in the source code (part):
/** * Returns The current value of the running Java Vsan ' s * high-resolution time source, in Nanosecon DS. * * <p>this method can only being used to measure elapsed time and was * not related to any other notion of syst EM or wall-clock time.
Two, the method of the construction of the parameter (set seed)
Syntax: Random ran = random (long seed)
The source code for the method of constructive construction is as follows:
1 /**2 * Creates a new random number generator using a single {@codelong} seed.3 * The seed is the initial value of the internal state of the pseudorandom4 * Number generator which is maintained by method {@link#next}.5 *6 * <p>the Invocation {@codenew Random (Seed)} is equivalent to:7 * <pre> {@code8 * Random rnd = new Random ();9 * Rnd.setseed (SEED);} </pre>Ten * One * @paramseed the initial seed A * @see#setSeed (Long) - */ - PublicRandom (Longseed) { the if(getclass () = = Random.class) - This. Seed =NewAtomiclong (initialscramble (Seed)); - Else { - //subclass might have overriden setseed + This. Seed =NewAtomiclong (); - setseed (seed); + } A } at - Private Static LongInitialscramble (Longseed) { - return (seed ^ multiplier) & mask; -}
The multiplier and mask are fixed values:
1 Private Static Final long multiplier = 0x5deece66dl; 2 3 Private Static Final long mask = (1L << 48)-1;
Third, the Code test
Two methods are used to generate random integers in [0, 100), each generating five groups, each group of 10 random numbers:
1 ImportJava.util.Random;2 3 Public classRandomtest {4 Public Static voidMain (string[] args) {5Randomtest RT =Newrandomtest ();6 rt.testrandom ();7 }8 9 Public voidTestrandom () {TenSYSTEM.OUT.PRINTLN ("Random does not set seed:"); One for(inti = 0; I < 5; i++) { ARandom random =NewRandom (); - for(intj = 0; J < 10; J + +) { -System.out.print ("" + random.nextint (100) + ","); the } -System.out.println (""); - } - +System.out.println (""); - +System.out.println ("Random Set Seed:"); A for(inti = 0; I < 5; i++) { atRandom random =NewRandom (); -Random.setseed (100); - for(intj = 0; J < 10; J + +) { -System.out.print ("" + random.nextint (100) + ","); - } -System.out.println (""); in } - } to}
The results of the operation are as follows:
Conclusion:
Although both are pseudo-random, however, The method of non-parametric construction (without seeding) has more randomness and can satisfy the random number requirement in general statistics. Using the constructive method of the argument (seeding) no matter how many times you generate, every random sequence generated is the same , a veritable pseudo-random!!
The function of Java:random and its seed