Search code examples
c++algorithmrandomplaying-cardsmersenne-twister

Properly seeding a RNG for a card game


I'm working on a card game and I need the shuffle algorithm to do a very good job and to be different every time the game runs and to not have predictable card sequences.

I'm using the Mersenne twister algorithm but it still needs a seed, so really, although it produces great numbers, right now there are only 1000 possible sequences of games since I'm using time(NULL) to seed. How should I be seeding?


Solution

  • My standard seeding technique:

    1. If /dev/urandom exists, read a seed from there.

    2. If you're in Windows, use CryptGenRandom().

    3. If all else fails, use time().

    (Not sure where your Mersenne twister comes from, but there new standard library has one in <random> which integrates very elegantly.)

    I'm happy to hear suggestions for platforms that aren't covered by the first two steps!