Connecting Tech Pros Worldwide Help | Site Map

random number generator of Math.random() ?

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 30th, 2006, 10:05 AM
Bart Vandewoestyne
Guest
 
Posts: n/a
Default random number generator of Math.random() ?

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

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

  #2  
Old May 30th, 2006, 02:35 PM
Oliver Wong
Guest
 
Posts: n/a
Default Re: random number generator of Math.random() ?


"Bart Vandewoestyne" <MyFirstName.MyLastName@telenet.be> wrote in message
news:1148983514.955928@seven.kulnet.kuleuven.ac.be ...[color=blue]
> Simple question: does the Java standard specify what random
> number generator algorithm should be used for Math.random() ?[/color]

Yes it does.
[color=blue]
> If
> 'no', then can anybody tell me what PRNG is behind Sun's version
> of Math.random()?[/color]

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

- Oliver

  #3  
Old May 30th, 2006, 03:45 PM
Bart Vandewoestyne
Guest
 
Posts: n/a
Default Re: random number generator of Math.random() ?

On 2006-05-30, Oliver Wong <owong@castortech.com> wrote:[color=blue]
>[color=green]
>> 'no', then can anybody tell me what PRNG is behind Sun's version
>> of Math.random()?[/color]
>
> See http://java.sun.com/j2se/1.5.0/docs/...il/Random.html[/color]

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/...ml#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

--
"Share what you know. Learn what you don't."
  #4  
Old May 30th, 2006, 09:05 PM
Oliver Wong
Guest
 
Posts: n/a
Default Re: random number generator of Math.random() ?


"Bart Vandewoestyne" <MyFirstName.MyLastName@telenet.be> wrote in message
news:1149003693.245496@seven.kulnet.kuleuven.ac.be ...
[...][color=blue]
> public double nextDouble() {
> return (((long)next(26) << 27) + next(27))
> / (double)(1L << 53);
> }
>[/color]
[...][color=blue]
> So am I correct if I say that the Java standard specifies the above PRNG
> as the PRNG to use when implementing Math.random() ?[/color]

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

- Oliver

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.