开发者

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 eve开发者_运维百科ry 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:

  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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜