Skip to content

Commit 63f3538

Browse files
committed
Use c++11 mt19937_64 as random number generator.
1 parent 6cbbf68 commit 63f3538

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/port.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
#include <unistd.h>
66
#include <sys/resource.h>
77

8-
/*
9-
* Return a pseudo-random number in the range 0 .. n-1
10-
*/
11-
// FIXME: drand48 doesn't provide enough random bits for 64bit LPC int.
12-
long random_number(long n)
13-
{
14-
static char called = 0;
8+
#include <random>
159

16-
if (!called) {
17-
time_t tim;
10+
// Returns a pseudo-random number in the range 0 .. n-1
11+
int64_t random_number(int64_t n)
12+
{
13+
static bool called = 0;
14+
static std::mt19937_64 engine;
1815

19-
time(&tim);
20-
srand48(tim);
16+
if(!called) {
17+
std::random_device rd;
18+
engine.seed(rd());
2119
called = 1;
22-
} /* endif */
23-
return (long)(drand48() * n);
20+
}
21+
22+
std::uniform_int_distribution<int64_t> dist(0, n-1);
23+
return dist(engine);
2424
}
2525

2626
/*

src/port.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* port.c
66
*/
77
#ifndef _FUNC_SPEC_
8-
long random_number(long);
8+
int64_t random_number(int64_t);
99
long get_current_time(void);
1010
const char *time_string(time_t);
1111
void get_usec_clock(long *, long *);

0 commit comments

Comments
 (0)