Skip to content

Commit 0ee887c

Browse files
committed
Use clock_gettime() and mpz_class for random seed.
clock_gettime is part of POSIX and recommended over gettimeofday. Also itreturns a higher precision, and mpz_class allows storing it, so we can seed libGMP with more randomness.
1 parent 3b157ad commit 0ee887c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

libchecktestdata.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
It's licensed under the 2-clause BSD license, see the file COPYING.
55
*/
66

7+
#define _XOPEN_SOURCE 700
8+
79
#include <iostream>
810
#include <iomanip>
911
#include <fstream>
@@ -19,7 +21,7 @@
1921
#include <cstdarg>
2022
#include <climits>
2123
#include <getopt.h>
22-
#include <sys/time.h>
24+
#include <time.h>
2325
#include <cstdlib>
2426
#include <boost/variant.hpp>
2527
#include <boost/exception_ptr.hpp>
@@ -1293,11 +1295,10 @@ void init_checktestdata(std::istream &progstream, int opt_mask)
12931295
}
12941296

12951297
// Initialize random generators
1296-
struct timeval time;
1297-
unsigned long seed;
1298-
gettimeofday(&time,NULL);
1299-
seed = (time.tv_sec * 1000) + (time.tv_usec % 1000);
1300-
srandom(seed);
1298+
struct timespec time;
1299+
clock_gettime(CLOCK_REALTIME,&time);
1300+
mpz_class seed = 1000000000 * mpz_class(time.tv_sec) + time.tv_nsec;
1301+
srandom(seed.get_ui());
13011302
gmp_rnd.seed(seed);
13021303

13031304
// Initialize current position in program and data.

0 commit comments

Comments
 (0)