[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Where to get a good seed for srandom()


that is an interesting (off-)topic :-) May I ask the very last question?

If % is not good enough for getting random values in a range, then what is?

I see a lot of "arc4random() % ..." when grepping the /usr/src on OpenBSD.

And my (probably too naive) approach to shuffling 32 cards has been:

        void shuffle(card deck[])
                unsigned        k, n;
                card               tmp;

                for (k = 0; k < 32; k++) {
                        /* find the new position for the card at k */
                        n = arc4random() % 32;

                        /* swap the cards at the indices k and n */
                        tmp = deck[n];
                        deck[n] = deck[k];
                        deck[k] = tmp;


2005/7/18, Jack Bates <jack_(_at_)_floatingdoghead_(_dot_)_net>:
> I happen to be very familiar with this subject, having developed, analyzed
> and qualified two different card-shuffling algorithms for real-money play
> in the online card game business.  There is a fair bit of literature on
> this subject.
> Some tips that I can share without breaking an NDA:
> 1) Do not use current time other than as an _adjunct_ to initial seeding
> of a quality PRNG.  Under OpenBSD, you can obtain a good initial seed from
> /dev/?random, so why bother using time at all?
> 2) Use of a hardware device (HRNG), such as the SG-100 from Protego
> (Sweden) or motherboard HRNG is preferred to a PRNG, as its state cannot
> be observed in memory.
> 3) Qualify the quality of your random seed if it is coming from an HRNG.
> Some of these things have "warm-up time" and will initially produce poor
> entropy.
> 4) Do not use the % (modulo) operator to select a card.  The residues from
> % introduce small amounts of bias, and this is a disqualifying factor for
> regulated gaming.
> 5) Knuth's "Art of Computer Programming" has an algorithm for fairly
> shuffling cards.  Unfortunately it assumes perfect floating-point math,
> which a computer cannot really do.  The algorithm, however, is worth
> studying.
> --
> Jack Bates
> Venice, CA, USA
> I play Texas Hold'Em at http://www.fulltiltpoker.com