Is it possible to include a starting seed with the Math.random method in Java? If so, what would the statement look like? I know how to include a starting seed in the Random class but how do I do this if I want to use the Math.random method instead of using the Random class?
-
From [the docs](http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random--), I'd say you can't: it creates a new Random() object on first invocation using the default constructor so you're stuck with whatever the default seed is for the no-argument Random() constructor - probably the current time. – Rup Oct 24 '14 at 00:34
-
1@Rup: "When this method is first called". Not each time. But yeah, you're stuck with it. If you need more control, you should use `Random` - `Math.random` is a convenience for those who don't want to bother. – Amadan Oct 24 '14 at 00:35
-
So if I am using the Math.random method there is no way for me to make the program execute the same set of random values each execution? – Oct 24 '14 at 00:36
-
1You might be able to manage it by hacking a seeded random number generator into the Math class by reflection. But it would be easier to just use `Random()` instead. – Rup Oct 24 '14 at 00:38
2 Answers
The JavaDoc to Math.random()
reads (in part),
When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression
new java.util.Random()
So, I believe you have to use the Random(long)
constructor (or call Random.setSeed(long)
) if you want to set a seed.

- 1
- 1

- 198,278
- 20
- 158
- 249
There is no way to explicitly seed Math.random, it gets a new Random() instance the first time it is called using the system clock as the seed.
Do note that seeding both Math.random and Random is to a certain extent an exercise in futility. Math.random is a front end to Random(). Random() is a linear congruential generator which produces a predictable sequence of numbers if you know any one of the numbers generated by the sequence. Neither Math.random nor Random produces numbers that are suitable for applications that need unpredictability. For that matter, a LCG is not even completely statistically random. The only thing it has going for it is that it is very, very fast and thus useful for things like, say, distributing hash values, where predictability is irrelevant (or even a plus) and somewhat-random is "good enough".
http://en.wikipedia.org/wiki/Linear_congruential_generator
If you need a cryptographically random number (i.e., one that is not predictable even if you know the previous number that was generated), please use a cryptography library's implementation of a PRNG, not Random() or Math.random. See the JCA SecureRandom class. The Internet will thank you for not adding to the large variety of security holes that afflict software where people made the mistake of thinking Random() or Math.random generated a truly random stream of numbers.

- 440
- 3
- 8
-
You assume OP is doing something that requires cryptographically secure random numbers, when there's nothing in the question to indicate anything of the sort. – David Conrad Oct 24 '14 at 02:07
-
I said *IF* he needs a cryptographically random number. I made no assumption about whether he needs one or not. Please learn how to read. And it is very rude to downvote an answer simply because you do not understand the meaning of the word "if". – eric.green Oct 25 '14 at 02:27
-
I didn't downvote your answer, I do understand the meaning of the word if, but the tone of your answer suggests that typical uses of random number generators need security. Some do, but LCGs are fine for many uses e.g., games. – David Conrad Oct 25 '14 at 02:44