sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Bart Vandewoestyne's Avatar

random number generator of Math.random() ?


Question posted by: Bart Vandewoestyne (Guest) on May 30th, 2006 11:05 AM
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."
3 Answers Posted
Oliver Wong's Avatar
Guest - n/a Posts
#2: 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...til/Random.html

- Oliver

Bart Vandewoestyne's Avatar
Bart Vandewoestyne May 30th, 2006 04:45 PM
Guest - n/a Posts
#3: 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...til/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...tml#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."
Oliver Wong's Avatar
Guest - n/a Posts
#4: 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

 
Not the answer you were looking for? Post your question . . .
196,956 members ready to help you find a solution.
Join Bytes.com

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 196,956 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors