Properly seeding a RNG for a card game
-
26-10-2019 - |
题
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?
解决方案
My standard seeding technique:
If
/dev/urandom
exists, read a seed from there.If you're in Windows, use
CryptGenRandom()
.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!
其他提示
You can use the operating system's entropy source to get a good random number seed. On Windows, that's CryptoAPI; on POSIX, pull bytes from /dev/urandom
.
A typical seed value is the low 32 bits in a 64 bit current time. For example Use the return value of Linux gettimeofday call.