forked from pholme/tsir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcg_rnd.c
51 lines (36 loc) · 1.26 KB
/
pcg_rnd.c
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
47
48
49
50
51
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// code for temporal network SIR by Petter Holme (2018)
// this file contains the random number generator, derived from the PCG
// RNG v0.94 http://www.pcg-random.org under the Apache License 2.0
// http://www.apache.org/licenses/LICENSE-2.0
// 32-bit Output, 64-bit State, the PCG-XSH-RR version
#include "tsir.h"
extern GLOBALS g;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uint32_t pcg_32 () {
uint64_t state = g.state;
uint32_t value, rot;
g.state = g.state * 6364136223846793005ull + 1442695040888963407ull;
value = ((state >> 18u) ^ state) >> 27u;
rot = state >> 59u;
return (value >> rot) | (value << ((- rot) & 31u));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uint32_t pcg_32_bounded (uint32_t bound) {
uint32_t threshold = -bound % bound, r;
do r = pcg_32();
while (r < threshold);
return r % bound;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uint16_t pcg_16 () {
static uint32_t exist, rmem;
if (exist) {
exist = 0;
return rmem >> 16;
}
exist = 1;
rmem = pcg_32();
return rmem;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -