2

Will two java.security.SecureRandom instances which are seeded with the same value initially give the same sequence of random numbers?

I am asking this because I want the same sequence of random numbers in both the client and the server. What if both of them are using the same seed value. Will the sequence be the same, or is there any way that the sequence can be made the same?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Ashwin
  • 12,691
  • 31
  • 118
  • 190

2 Answers2

8

From the API docs:

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

Taymon
  • 24,950
  • 9
  • 62
  • 84
  • 1
    please read the question carefully I am using secure random and you are talking about random. – Ashwin Mar 08 '12 at 00:56
  • 1
    `SecureRandom` is a child class of `Random`, I believe this quote from the API still applies. By the definition of a seed, it would follow that two random generators with the same seed would provide the same output - that's the entire point of the seed. – charlemagne Mar 08 '12 at 01:03
  • 1
    Just to be clear, the behavior of `java.security.SecureRandom` is depends on the selected implementation, while the behavior of `java.util.Random` is as described. – trashgod Mar 08 '12 at 05:20
2

What if both of them are using the same seed value. Will the sequence be the same?

No, they definitely won't. At least not in Oracle's Java 7 SDK implementation. See my sample code in this SO post. It appears that the implementation may elect to use additional sources of randomness, in addition to the provided seed.

Community
  • 1
  • 1
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331