Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

random number generator of Math.random() ?

Thread view: 
Bart Vandewoestyne - 30 May 2006 11:05 GMT
Simple question: does the Java standard specify what random
number generator algorithm should be used for Math.random() ?  If
'no', then can anybody tell me what PRNG is behind Sun's version
of Math.random()?

Thanks,
Bart

Signature

    "Share what you know.  Learn what you don't."

Oliver Wong - 30 May 2006 15:28 GMT
> Simple question: does the Java standard specify what random
> number generator algorithm should be used for Math.random() ?

   Yes it does.

>  If
> 'no', then can anybody tell me what PRNG is behind Sun's version
> of Math.random()?

   See http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html

   - Oliver
Bart Vandewoestyne - 30 May 2006 16:41 GMT
>> 'no', then can anybody tell me what PRNG is behind Sun's version
>> of Math.random()?
>
>     See http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html

Hmm... so if I understand things correctly, the pseudo-random
numbers produced by Random.nextDouble() are the same as the ones
produced by Math.random() and the underlying algorithm is the one
as specified in

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html#nextDouble()

namely

public double nextDouble() {
      return (((long)next(26) << 27) + next(27))
          / (double)(1L << 53);
}

Looking at Random.next() then tells me that the PRNG used here 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

So am I correct if I say that the Java standard specifies the above PRNG as the PRNG to use when implementing Math.random() ?

Bart

Signature

    "Share what you know.  Learn what you don't."

Oliver Wong - 30 May 2006 22:01 GMT
[...]
> public double nextDouble() {
>       return (((long)next(26) << 27) + next(27))
>           / (double)(1L << 53);
> }

[...]
> So am I correct if I say that the Java standard specifies the above PRNG
> as the PRNG to use when implementing Math.random() ?

   Yes, I think so, or at least one which returns the exact same set of
numbers given the same initial seed.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.