forked from pholme/sir
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpcg_rnd.c
More file actions
76 lines (56 loc) · 1.7 KB
/
Copy pathpcg_rnd.c
File metadata and controls
76 lines (56 loc) · 1.7 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 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: PCG-XSH-RS
#include "sir.h"
extern GLOBALS g;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void pcg_init ( uint64_t initstate ) {
int fd;
unsigned long sz;
g.state = (initstate + 1442695040888963407ULL) * 6364136223846793005ULL + 1442695040888963407ULL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uint32_t pcg_32 () {
uint64_t state = g.state;
uint32_t value, rot;
g.state = g.state * 6364136223846793005ULL + 1442695040888963407ULL;
value = (uint32_t)(((state >> 18u) ^ state) >> 27u);
rot = state >> 59u;
return (value >> rot) | (value << ((- rot) & 31));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uint32_t pcg_32_bounded (uint32_t bound) {
uint32_t threshold = -bound % bound, r;
for ( ; ; ) {
r = pcg_32();
if (r >= threshold) return r % bound;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uint16_t pcg_16 () {
static unsigned int exist;
if (exist) {
exist = 0;
return g.rmem >> 16;
}
exist = 1;
g.rmem = pcg_32();
return (uint16_t) g.rmem;
}
uint8_t pcg_8() {
static unsigned int n=0 ;
static uint32_t r ;
if (n == 0) {
r = pcg_32() ;
n = 4 ;
}
else {
r /= 0x100 ;
n-- ;
}
return (uint8_t) (r & 0xff);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -