I was wondering what the default seed for the PRNG* behind Math.random()
in Java is. From what I understand the one in C is based upon the system clock. So is it similar in Java? Also, is the seed changed everytime Math.random()
is called?
*PRNG = Pseudo Random Number Generator
You can always read the code.
Math.random()
just uses an internal static Random object thats instantiated with no args...If you Read The Fine Manual it tells you
Following up with
java.util.Random()
, the documentation saysThe current implementation appears to be based on
System.nanoTime()
but could change and still be compliant with the documentation's contract.As for changing the seed with every call, that's not how seeds work. PRNGs are seeded once, and then produce a sequence of values that evolve from that initial state. You shouldn't, and Java doesn't, keep re-seeding.
As you can see on documentation, the function uses a class called Random(), wich uses a 48-bit seed, and generate a uniform distribution.