Correttamente semina un RNG per un gioco di carte
-
26-10-2019 - |
Domanda
Sto lavorando su un gioco di carte e ho bisogno l'algoritmo di riordino di fare un ottimo lavoro e di essere diverso ogni volta che viene eseguito il gioco e di non avere sequenze di carte prevedibili.
Sto utilizzando l'algoritmo Mersenne Twister, ma ha ancora bisogno di un seme, in modo davvero, anche se produce grandi numeri, in questo momento ci sono solo 1000 possibili sequenze di gioco dal momento che sto usando ora (NULL) alle sementi. Come dovrei semina?
Soluzione
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!
Altri suggerimenti
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.