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?

È stato utile?

Soluzione

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!

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top