-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.c
More file actions
42 lines (33 loc) · 1.05 KB
/
Copy pathrandom.c
File metadata and controls
42 lines (33 loc) · 1.05 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
#include "random.h"
// PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org
// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website)
static prng_state s_prng_state = {
0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL, NAN
};
void prng_seed_r(prng_state* rng, u64 initstate, u64 initseq){
rng->state = 0U;
rng->inc = (initseq << 1u) | 1u;
prng_rand_r(rng);
rng->state += initstate;
prng_rand_r(rng);
rng -> prev_norm = NAN;
}
void prng_seed(u64 initstate, u64 initseq){
prng_seed_r(&s_prng_state, initstate, initseq);
}
u32 prng_rand_r(prng_state* rng){
uint64_t oldstate = rng->state;
rng->state = oldstate * 6364136223846793005ULL + rng->inc;
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
uint32_t rot = oldstate >> 59u;
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}
u32 prng_rand(void){
return prng_rand_r(&s_prng_state);
}
f32 prng_randf_r(prng_state*rng){
return (f32) prng_rand_r(rng) / (f32)UINT32_MAX;
}
f32 prng_randf(){
return prng_randf_r(&s_prng_state);
}