Velocity Reviews > Java > Math.random()

# Math.random()

Ike
Guest
Posts: n/a

 03-07-2006
Any idea using Math.random(), how long it takes to start to cycle? I'm
running upwards of ten million iterations at a crack, using Math.random() on
each itreration and am wondering if this is actually random enough for my
purposes. Thanks, Ike

Oliver Wong
Guest
Posts: n/a

 03-07-2006
"Ike" <(E-Mail Removed)> wrote in message
news:y2kPf.1256\$(E-Mail Removed) k.net...
> Any idea using Math.random(), how long it takes to start to cycle? I'm
> running upwards of ten million iterations at a crack, using Math.random()
> on
> each itreration and am wondering if this is actually random enough for my
> purposes. Thanks, Ike

Math.random() internally uses the java.util.Random class. From the
documentation for java.util.Random:

<quote>
The class uses a 48-bit seed, which is modified using a linear congruential
formula. (See Donald Knuth, The Art of Computer Programming, Volume 2,
Section 3.2.1.)
</quote>

So do some research to try to calculate what the period is.

Also, the behaviour of Random.nextDouble() (which is what Math.random()
uses), has changed recently.

<quote>
In early versions of Java, the result was incorrectly calculated as:

return (((long)next(27) << 27) + next(27))
/ (double)(1L << 54);

This might seem to be equivalent, if not better, but in fact it introduced a
large nonuniformity because of the bias in the rounding of floating-point
numbers: it was three times as likely that the low-order bit of the
significand would be 0 than that it would be 1!
</quote>

- Oliver

Daniel Dyer
Guest
Posts: n/a

 03-07-2006
On Tue, 07 Mar 2006 18:12:46 -0000, Ike <(E-Mail Removed)> wrote:

> Any idea using Math.random(), how long it takes to start to cycle? I'm
> running upwards of ten million iterations at a crack, using
> Math.random() on
> each itreration and am wondering if this is actually random enough for my
> purposes. Thanks, Ike

See Roedy's page (http://mindprod.com/jgloss/pseudorandom.html) for more
information about random numbers in Java.

I'm not sure about the period of java.util.Random (which you "should" use
instead of Math.random()). I would guess that it's well in excess of 10
million, but it has other problems besides the period.

Have a look at this thread from a couple of days ago, which drifted into a
discussion about random numbers in Java:

The Mersenne Twister mentioned there has a period of 2^19937-1 according
to the inventors, which is quite a bit more than you would need. As a
bonus, the output is statistically more random and it's faster than
java.util.Random.

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk

Roedy Green
Guest
Posts: n/a

 03-07-2006
On Tue, 07 Mar 2006 19:33:06 -0000, "Daniel Dyer"
<(E-Mail Removed)> wrote, quoted or indirectly
quoted someone who said :

>I'm not sure about the period of java.util.Random (which you "should" use
>instead of Math.random()). I would guess that it's well in excess of 10
>million, but it has other problems besides the period.

do an experiment. Generate Integer.MAX_VALUE random numbers. Track
which ones appear with a Java.util.BitSet. Track only teh low order
1,000,000.

When done see what fraction of the numbers are used and compare that
with the expected value for perfect random.
--
http://mindprod.com Java custom programming, consulting and coaching.

Daniel Dyer
Guest
Posts: n/a

 03-08-2006
On Tue, 07 Mar 2006 22:48:41 -0000, Roedy Green
<(E-Mail Removed) > wrote:

> On Tue, 07 Mar 2006 19:33:06 -0000, "Daniel Dyer"
> <(E-Mail Removed)> wrote, quoted or indirectly
> quoted someone who said :
>
>> I'm not sure about the period of java.util.Random (which you "should"
>> use
>> instead of Math.random()). I would guess that it's well in excess of 10
>> million, but it has other problems besides the period.

>
> do an experiment. Generate Integer.MAX_VALUE random numbers. Track
> which ones appear with a Java.util.BitSet. Track only teh low order
> 1,000,000.
>
> When done see what fraction of the numbers are used and compare that
> with the expected value for perfect random.

That will give you an indication of the distribution but not the period.

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk

Roedy Green
Guest
Posts: n/a

 03-08-2006
On Wed, 08 Mar 2006 00:23:13 -0000, "Daniel Dyer"
<(E-Mail Removed)> wrote, quoted or indirectly
quoted someone who said :

>That will give you an indication of the distribution but not the period.

I think you can still make some inferences, at least a bound.

if you discovered 1/3 of numbers were used, you could presume the
periodicity was rangeofint/3.

I think these things are desined to cycle through some subset of int
without repetition.
--
http://mindprod.com Java custom programming, consulting and coaching.

Kenneth P. Turvey
Guest
Posts: n/a

 03-12-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, 07 Mar 2006 18:12:46 +0000, Ike wrote:

> Any idea using Math.random(), how long it takes to start to cycle? I'm
> running upwards of ten million iterations at a crack, using Math.random() on
> each itreration and am wondering if this is actually random enough for my
> purposes. Thanks, Ike

You'll be fine. It has a much longer period than that. I never use
Math.random() though. I always use Random.nextDouble(). I would be
more concerned about how it is seeded. I just checked and it does the
sensible thing.

- --
Kenneth P. Turvey <(E-Mail Removed)>
Phone : (314) 255-2199

XMPP IM: http://www.velocityreviews.com/forums/(E-Mail Removed)
Yahoo IM: kpturvey2
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFEE7ePi2ZgbrTULjoRAr6OAKDV+OsG/6DLztlczDcwVGlYUn2t3ACdGXNl
MKG8/xcDeoPvrrjYNfFqTlk=
=xcfc
-----END PGP SIGNATURE-----

Luc The Perverse
Guest
Posts: n/a

 03-12-2006
"Kenneth P. Turvey" <(E-Mail Removed)> wrote in message
newsan.2006.03.12.05.54.30.85917@squeakydolphin. com...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Tue, 07 Mar 2006 18:12:46 +0000, Ike wrote:
>
>> Any idea using Math.random(), how long it takes to start to cycle? I'm
>> running upwards of ten million iterations at a crack, using Math.random()
>> on
>> each itreration and am wondering if this is actually random enough for my
>> purposes. Thanks, Ike

I missed this original post. To the OP.

If you are looking for a really good PRNG I suggest ISAAC. You will find an
implementation of it here http://burtleburtle.net/bob/rand/isaacafa.html

--
LTP