Default PRNG seed in Java

I was wondering that the default is the default seed for PRNG * behind Math.random() in Java. From what I understand, the one in C is based on the system clock. So does it look like Java? Also, does the seed change every time Math.random() is Math.random() ?

* PRNG = Pseudo Random Number Generator

+3
java random prng
source share
3 answers

If you read the exact manual , it informs you

When this method is first called, it creates one new pseudo-random number generator, just as if the expression

new java.util.Random()

This new pseudo-random number generator is then used for all calls to this method and is not used anywhere else.

According to java.util.Random() , the documentation states

public Random()

Creates a new random number generator. This constructor sets the seed of a random number generator to a value that is likely to be different from any other call to this constructor.

The current implementation seems to be based on System.nanoTime() , but may change and still conform to the documentation contract.

As for changing the seed with every call, this is not how the seeds work. PRNGs are plated once and then produce a sequence of values ​​that evolve from this initial state. You do not have to, and Java - no, continue re-sowing.

+3
source share

You can always read the code .

Math.random() simply uses the internal static Random object, which is instantiated with no arguments ...

  Random() { 90 this(seedUniquifier() ^ System.nanoTime()); 91 } 92 93 private static long More ...seedUniquifier() { 94 // L'Ecuyer, "Tables of Linear Congruential Generators of 95 // Different Sizes and Good Lattice Structure", 1999 96 for (;;) { 97 long current = seedUniquifier.get(); 98 long next = current * 181783497276652981L; 99 if (seedUniquifier.compareAndSet(current, next)) 100 return next; 101 } 102 } 103 
+4
source share

As you can see in the documentation , the function uses the Random () class, which uses a 48-bit seed and generates a uniform distribution.

0
source share

All Articles