Home arrow static arrow Java Programming [Archive] - Java random number generator
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Java random number generator
This topic has 8 replies on 1 page.

Posts:4
Registered: 6/16/04
Java random number generator  
Jun 16, 2004 6:22 AM



 
Hi,

Is the Java random number generator always guaranteed to generate identical sequences of random numbers on different Java virtual machine implementation?

Nizam
 

Posts:4,000
Registered: 24.02.01
Re: Java random number generator  
Jun 16, 2004 6:46 AM (reply 1 of 8)



 
Since Random is an API class, I'd say no, it always produces the same sequence for the same seed.
 

Posts:4
Registered: 6/16/04
Re: Java random number generator  
Jun 16, 2004 6:55 AM (reply 2 of 8)



 
Since Random is an API class, I'd say no, it always
produces the same sequence for the same seed.

So if I'm using the same seed for my random number generator, I'll getting the same sequence of random number even though I'm implement my random number generator at different JVM?
 

Posts:3,055
Registered: 18/06/98
Re: Java random number generator  
Jun 16, 2004 6:58 AM (reply 3 of 8)



 
This was extracted of the JLS 1.0 - the javadoc of java.util.Random. Believe it or not - it was extracted from the October 2001 edition of the MSDN Library (Microsoft Developer Network).


protected int next(int bits)

The general contract of next is that it returns an int value and if the argument bits is between 1 and 32 (inclusive), then that many low-order bits of the returned value will be (approximately) independently chosen bit values, each of which is (approximately) equally likely to be 0 or 1.

The method next is implemented by class Random as follows:

synchronized protected int next(int bits) {	seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);	return (int)(seed >>> (48 - bits));}

This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.


If you get the source of the J2SDK 1.5.0 the javadoc doesn't changed.

----

The behaviour of java.util.Random is defined in the JLS (it is carved in stone), but the behaviour of java.security.SecureRandom is not, and depends on your JCE provider. It could be generated by a true hardware random number generated, if your JCE provider supports it and you have a hardware random number generator in your machine.
 

Posts:4,000
Registered: 24.02.01
Re: Java random number generator  
Jun 16, 2004 7:00 AM (reply 4 of 8)



 
So if I'm using the same seed for my random number generator, I'll getting the same sequence of
random number even though I'm implement my random number generator at different JVM?

If you use java.util.Random? Yes.
 

Posts:4
Registered: 6/16/04
Re: Java random number generator  
Jun 16, 2004 7:05 AM (reply 5 of 8)



 
So if I'm using the same seed for my random number
generator, I'll getting the same sequence of
random number even though I'm implement my random
number generator at different JVM?

If you use java.util.Random? Yes.

Did you got any references that published this fact?
 

Posts:3,055
Registered: 18/06/98
Re: Java random number generator  
Jun 16, 2004 7:26 AM (reply 6 of 8)



 
Mr. Nizam,
just read the javadoc of java.util.Random that I've posted above. It defines explicitly the formula used by the java.util.Random methods, and as I said above, it's "carved in stone" because even Microsoft implemented their version of the JDK using the JLS specification. (search for "Java Language Specification" in the java.sun.com site...)

- but -

obviously you need to set up the java.util.Random with a fixed seed. If you use the default behaviour, it will generate a random sequence that depends on the System.currentTimeMillis() current value...
 

Posts:5,965
Registered: 5/17/03
Re: Java random number generator  
Jun 16, 2004 7:34 AM (reply 7 of 8)



 
If you use the default behaviour, it
will generate a random sequence that depends on the
System.currentTimeMillis() current value...

In version 1.5 this isn't specified anymore, but I don't know what's used instead.
 

Posts:4
Registered: 6/16/04
Re: Java random number generator  
Jun 16, 2004 7:42 AM (reply 8 of 8)



 
Mr. Nizam,
just read the javadoc of java.util.Random that I've
posted above. It defines explicitly the formula used
by the java.util.Random methods, and as I said above,
it's "carved in stone" because even Microsoft
implemented their version of the JDK using the JLS
specification. (search for "Java Language
Specification" in the java.sun.com site...)

- but -

obviously you need to set up the java.util.Random with
a fixed seed. If you use the default behaviour, it
will generate a random sequence that depends on the
System.currentTimeMillis() current value...

Thanks.. but I'm not really understand about it...

 
This topic has 8 replies on 1 page.