1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-11-03 17:27:29 -05: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 <ctype.h>
#include <string.h>
#include <time.h>
#include <errno.h>
@ -68,6 +67,7 @@
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <monetary.h>

View File

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