Random Number Generation

Discussion in 'Java' started by adrian.bartholomew@gmail.com, Sep 12, 2008.

  1. Guest

    public Deck() {
    reset();
    try {
    random = SecureRandom.getInstance(RANDOM_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
    throw new NoSecureRandomException(e);
    }
    }

    public final void reset() {
    cards.clear();
    cards.addAll(Card.allCards);
    }

    public void shuffle() {
    for (int i=0; i<10; i++) {
    random.setSeed(System.currentTimeMillis());
    Collections.shuffle(cards, random);
    }
    }
    ......

    The above code works, except that after a while i begin to see the
    same patterns in hands dealt.
    I can predict what the other hands would hold and can continue with
    correct anticipatory play.
    This should not be.
    As you can see, I have even tried shuffling 10 times each time.

    Any help would be appreciated.
     
    , Sep 12, 2008
    #1
    1. Advertising

  2. zerg Guest

    Matt Humphrey wrote:
    > <> wrote in message
    > news:...
    >>

    >
    > <snip code>
    >
    >> The above code works, except that after a while i begin to see the
    >> same patterns in hands dealt.
    >> I can predict what the other hands would hold and can continue with
    >> correct anticipatory play.
    >> This should not be.
    >> As you can see, I have even tried shuffling 10 times each time.

    >
    > SecureRandom (and Random) are pseudo-random and will reproduce the same
    > sequence for the same seed. Most likely your shuffle loop is so fast that
    > the clock you're using for seeds does not change between iterations. Many
    > PC-based millisecond clocks are only accurate to about 3 or 13 milliseconds,
    > which is actually a very long time for fitting in calculations. Secure
    > random includes a good pre-randomizer based on thread scheduling so try not
    > setting a seed at all or set the seed once when the PRNG is created.


    There's also System.nanoTime(), which is supposed to have a far higher
    resolution than currentTimeMillis().
     
    zerg, Sep 13, 2008
    #2
    1. Advertising

  3. Roedy Green Guest

    On Fri, 12 Sep 2008 15:47:31 -0700 (PDT),
    wrote, quoted or indirectly quoted someone who said :

    > random.setSeed(System.currentTimeMillis());


    don't do that. Set the seed only ONCE. If you don't want to
    reproduce results, you don't even need a seed.

    See http://mindprod.com/jgloss/pseudorandom.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 13, 2008
    #3
  4. wrote:
    > public void shuffle() {
    > for (int i=0; i<10; i++) {
    > random.setSeed(System.currentTimeMillis());
    > Collections.shuffle(cards, random);
    > }
    > }


    From experience, I can tell you that you've shuffled at most twice (if
    you're lucky), and probably once, assuming you have no more than 52
    cards. A millisecond is an awful long time for a computer, and the
    method does not in general have millisecond-scale resolution (10 and 16
    are common, IIRC).

    Resetting the seed so rapidly kills your entropy big time. Even if each
    shuffle takes 1 ms, you'd essentially only see changes in the last 4-6
    bits, throwing away several bits of good information. Don't reset the
    seed, you don't need to.

    Also, note that, although using the current time in milliseconds might
    be fair entropy across invocations, using it multiple times in the same
    invocation hurts entropy. The random number generator, without a seed,
    will generate its own without any problems, and it does so in a far
    smarter way.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 13, 2008
    #4
  5. Uwe Schmitt Guest

    On 13 Sep., 00:47, wrote:
    >     public Deck() {
    >         reset();
    >         try {
    >             random = SecureRandom.getInstance(RANDOM_ALGORITHM);
    >         } catch (NoSuchAlgorithmException e) {
    >             throw new NoSecureRandomException(e);
    >         }
    >     }
    >
    >     public final void reset() {
    >         cards.clear();
    >         cards.addAll(Card.allCards);
    >     }
    >
    >     public void shuffle() {
    >         for (int i=0; i<10; i++) {
    >             random.setSeed(System.currentTimeMillis());
    >             Collections.shuffle(cards, random);
    >         }
    >     }


    You should call setSeed only once before shuffling. You can move it
    outside the for loop, or into your constructor.

    Greetings, Uwe
     
    Uwe Schmitt, Sep 13, 2008
    #5
  6. Guest

    On Sep 12, 7:26 pm, Joshua Cranmer <> wrote:
    > wrote:
    > > public void shuffle() {
    > > for (int i=0; i<10; i++) {
    > > random.setSeed(System.currentTimeMillis());
    > > Collections.shuffle(cards, random);
    > > }
    > > }

    >
    > From experience, I can tell you that you've shuffled at most twice (if
    > you're lucky), and probably once, assuming you have no more than 52
    > cards. A millisecond is an awful long time for a computer, and the
    > method does not in general have millisecond-scale resolution (10 and 16
    > are common, IIRC).
    >
    > Resetting the seed so rapidly kills your entropy big time. Even if each
    > shuffle takes 1 ms, you'd essentially only see changes in the last 4-6
    > bits, throwing away several bits of good information. Don't reset the
    > seed, you don't need to.
    >
    > Also, note that, although using the current time in milliseconds might
    > be fair entropy across invocations, using it multiple times in the same
    > invocation hurts entropy. The random number generator, without a seed,
    > will generate its own without any problems, and it does so in a far
    > smarter way.
    >
    > --
    > Beware of bugs in the above code; I have only proved it correct, not
    > tried it. -- Donald E. Knuth


    Thanks guys. So you're saying that I DONT even need a seed? And that
    the seed itself is what's causing my predictability?
     
    , Sep 13, 2008
    #6
  7. wrote:
    > Thanks guys. So you're saying that I DONT even need a seed? And that
    > the seed itself is what's causing my predictability?


    More accurately, the problem is that you are reseeding unnecessarily and
    in such a manner that you are reseeding with the same value over and
    over again under the illusion that it will increase randomness.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 13, 2008
    #7
  8. Roedy Green Guest

    On Sat, 13 Sep 2008 10:30:47 -0700 (PDT),
    wrote, quoted or indirectly quoted someone who said :

    >Thanks guys. So you're saying that I DONT even need a seed? And that
    >the seed itself is what's causing my predictability?


    The only time you use a seed is if you want reproducibility. This can
    be helpful in debugging or when you want others to get the exact same
    results as you when they rerun.

    If you want random effects, don't use a seed at all. The class will
    start itself with a clever seed based on time and perhaps other random
    factors.

    Then there is something in between -- picking a "random" quote of the
    day, which should pick the same quote any time the computation is done
    that day. I talk about that too at
    http://mindprod.com/jgloss/pseudorandom.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 13, 2008
    #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. dpi

    Random Number Generation

    dpi, Mar 13, 2006, in forum: VHDL
    Replies:
    4
    Views:
    10,091
    vipinlal
    Mar 26, 2010
  2. ANM
    Replies:
    2
    Views:
    1,371
    Thomas Schodt
    Mar 7, 2004
  3. mescaline
    Replies:
    4
    Views:
    1,342
    Cy Edmunds
    Sep 10, 2003
  4. globalrev
    Replies:
    4
    Views:
    774
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,180
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page