Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Math.random()

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      03-07-2006
"Ike" <> wrote in message
news:y2kPf.1256$ 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>

That might affect your answer as well.

- Oliver

 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      03-07-2006
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


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:

http://groups.google.com/group/comp....788a5bba0fe310

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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-07-2006
On Tue, 07 Mar 2006 19:33:06 -0000, "Daniel Dyer"
<> 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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      03-08-2006
On Tue, 07 Mar 2006 22:48:41 -0000, Roedy Green
< > wrote:

> On Tue, 07 Mar 2006 19:33:06 -0000, "Daniel Dyer"
> <> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-08-2006
On Wed, 08 Mar 2006 00:23:13 -0000, "Daniel Dyer"
<> 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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
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 <kt->
Phone : (314) 255-2199

XMPP IM:
Yahoo IM: kpturvey2
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

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

 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      03-12-2006
"Kenneth P. Turvey" <kt-> 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57