1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-09-01 17:14:15 -04:00

Include microseconds as well as seconds for the random number seed

This commit is contained in:
John Zaitseff 2011-07-20 12:42:10 +10:00
parent c1b284e977
commit cbff5193d9
2 changed files with 11 additions and 7 deletions

View File

@ -60,7 +60,6 @@
#include <locale.h> #include <locale.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <errno.h> #include <errno.h>
@ -68,6 +67,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <monetary.h> #include <monetary.h>

View File

@ -262,13 +262,17 @@ void err_exit_nomem (void)
void init_rand (void) void init_rand (void)
{ {
/* Ideally, initialisation of the random number generator should be /* Ideally, initialisation of the random number generator should be
made using seed48() and lcong48(). However, we can only be made using seed48() and lcong48(). However, since this is "only a
assured of no more than 32 bits of "randomness" by using time(), game", 32 bits of "randomness" as returned by gettimeofday() is
available on all POSIX systems. If time_t is larger than long probably more than enough... */
int, we throw away the top bits. */
time_t curtime = time(NULL); // NB: time_t may be larger than long int struct timeval tv;
srand48((long int) curtime); unsigned long int seed;
gettimeofday(&tv, NULL); // If this fails, tv is random enough!
seed = tv.tv_sec * tv.tv_usec;
srand48(seed);
} }