Same value for seeds used to create java Random on two machines

If I use the same initial value for Random in a java program and run it on two different machines, do I get the same set of numbers?

eg

long seed = 123L;//may be taken from some database or something java.util.Random ran = new java.util.Random(seed); int ret = 0; for (int i= 0; i< 10; i++){ ret = ran.nextInt(1000); System.out.println("ret="+ret); } 

I always get

 ret=782 ret=450 ret=176 ret=789 ret=795 ret=657 ret=834 ret=837 ret=585 ret=453 

If I ran this several times on my computer , I would get the same set of numbers .. but suppose someone can get the secret value of the seed that I used (by guessing or from the secret location where it was saved) and run this code on his machine , will he get the same set of numbers?

+7
source share
4 answers

Yes, the contract defining the method of generating random numbers is the same in both cases, so they will receive the same sequence of numbers if they have the same seed. Random implementations must use the given algorithms to ensure that this is the case. A more accurate way to post it (from the relevant documentation):

If two instances of Random are created with the same seed, and for each of them the same sequence of method calls is performed, they will generate and return identical sequences of numbers. To guarantee these properties, certain algorithms are defined for the Random class. Java implementations should use all the algorithms shown here for class Random, for absolute portability of Java code. However, subclasses of the Random class are allowed to use other algorithms if they correspond to common contracts for all methods.

+8
source

Yes, that’s the point.

For example: in Minecraft, you can get level seeds to initialize a random number generator, and each person with this seed will receive the same card.

If you read JavaDoc , you will see that next(int bits) (and nextInt() just next(32) ) will update the seed to (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1) and return (int)(seed >>> (48 - bits)) . It is always the same on any computer for the same seed.

+2
source

The JRE should generate the same sequence of random numbers, given that they use the same seed, and the algorithm is used. The Java documentation on Random says:

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

Algorithms are also used by all JRE implementations:

To guarantee this property, specific algorithms are specified for the Random class. Java implementations should use all the algorithms shown here for the Random class for absolute portability of Java code. However, subclasses of the Random class are allowed to use other algorithms if they correspond to common contracts for all methods.

+2
source

The answer is yes, because the Random.nextInt () method is not possible. It just uses a seed and does some calculation to generate numbers. The code does not use any specific mahcine parameters to generate. Here is the nextInt () code:

  public int nextInt(int n) { if (n<=0) throw new IllegalArgumentException("n must be positive"); if ((n & -n) == n) // ie, n is a power of 2 return (int)((n * (long)next(31)) >> 31); int bits, val; do { bits = next(31); val = bits % n; } while(bits - val + (n-1) < 0); return val; } 

More on the Random nextInt method here:

http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt(int)

+1
source

All Articles