Reputation: 20136
I need to ensure that each user of a specified web page returns a list of objects in a random order. I need to ensure that if the page is reloaded, those objects stay in the same random order. Will the following method of initialising a Random number generator ensure I always get the same sequence of random numbers on a per user basis, no mater which JVM or version of JVM is being used?
Random random = new Random();
random.setSeed(username.hashCode());
We don't need true randomness, we just need to ensure that each user does not see the "Questions" in the same order.
Upvotes: 1
Views: 56
Reputation: 2833
From the Oracle Documentation:
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.
Thus, if two Random
objects are initialised with the same seed (such as your username.hashcode
), and the same sequence of calls is made to those objects, then they will always return identical results.
Upvotes: 2