Connecting Tech Pros Worldwide Help | Site Map

random number generator of Math.random() ?

  #1  
Old May 30th, 2006, 11:05 AM
Bart Vandewoestyne
Guest
 
Posts: n/a
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, 03:35 PM
Oliver Wong
Guest
 
Posts: n/a

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, 04:45 PM
Bart Vandewoestyne
Guest
 
Posts: n/a

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, 10:05 PM
Oliver Wong
Guest
 
Posts: n/a

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

Closed Thread