-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathrandom.c
More file actions
46 lines (37 loc) · 776 Bytes
/
random.c
File metadata and controls
46 lines (37 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "hash.h"
#include "random.h"
#include "util.h"
/* Maintain thread-independent random seed to prevent race. */
static __thread uint32_t seed = 0;
static uint32_t random_next(void);
void random_init(void)
{
while (!seed) {
uint32_t t = (uint32_t) time(NULL);
seed = t;
srand48(seed);
}
}
void random_set_seed(uint32_t seed_)
{
while (!seed_)
seed_ = (uint32_t) time(NULL);
seed = seed_;
srand48(seed_);
}
uint32_t random_uint32(void)
{
random_init();
return random_next();
}
static uint32_t random_next(void)
{
uint32_t *seedp = &seed;
*seedp ^= *seedp << 13;
*seedp ^= *seedp >> 17;
*seedp ^= *seedp << 5;
return *seedp;
}