Math.random()

Discussion in 'Java' started by Ike, Mar 7, 2006.

  1. Ike

    Ike Guest

    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
     
    Ike, Mar 7, 2006
    #1
    1. Advertising

  2. Ike

    Oliver Wong Guest

    "Ike" <> wrote in message
    news:y2kPf.1256$...
    > 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
     
    Oliver Wong, Mar 7, 2006
    #2
    1. Advertising

  3. Ike

    Daniel Dyer Guest

    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.lang.java.programmer/browse_thread/thread/b6788a5bba0fe310

    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
     
    Daniel Dyer, Mar 7, 2006
    #3
  4. Ike

    Roedy Green Guest

    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.
     
    Roedy Green, Mar 7, 2006
    #4
  5. Ike

    Daniel Dyer Guest

    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
     
    Daniel Dyer, Mar 8, 2006
    #5
  6. Ike

    Roedy Green Guest

    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.
     
    Roedy Green, Mar 8, 2006
    #6
  7. -----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 <>
    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-----
     
    Kenneth P. Turvey, Mar 12, 2006
    #7
  8. "Kenneth P. Turvey" <> wrote in message
    news:p...
    > -----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

    :)
     
    Luc The Perverse, Mar 12, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chirs
    Replies:
    18
    Views:
    800
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,182
    Jonathan Turkanis
    Apr 1, 2004
  3. Mark Healey
    Replies:
    7
    Views:
    1,572
    Tim Prince
    May 22, 2006
  4. globalrev
    Replies:
    4
    Views:
    825
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,340
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page