aboutsummaryrefslogtreecommitdiff
path: root/sys/libn/random.c
blob: 551d1e91946ce968b1e6d72854c2075496edcda8 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <u.h>
#include <libn.h>

// ----------------------------------------------------------------------------
// Internal structure

uint64 
rol64(uint64 x, int k)
{
	return (x << k) | (x >> (64 - k));
}

typedef struct Rng {
	uint64 s[4];
} Rng;

uint64
xoshiro256ss(Rng *state)
{
	uint64 *s     = state->s;
	uint64 result = rol64(s[1] * 5, 7) * 9;
	uint64 t      = s[1] << 17;

	s[2] ^= s[0];
	s[3] ^= s[1];
	s[1] ^= s[2];
	s[0] ^= s[3];

	s[2] ^= t;
	s[3] = rol64(s[3], 45);

	return result;
}

typedef struct Mix
{
	uint64 s;
} Mix;

uint64 
splitmix64(struct Mix *state) {
	uint64 result = state->s;

	state->s = result + 0x9E3779B97f4A7C15;
	result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9;
	result = (result ^ (result >> 27)) * 0x94D049BB133111EB;
	return result ^ (result >> 31);
}

static Rng RNG;

// ----------------------------------------------------------------------------
// Exported functions

/* Initializes the global RNG */

error
rng·init(uint64 seed)
{
	Mix smstate = {seed};

    for (int i=0; i < 4; i++) 
        RNG.s[i] = splitmix64(&smstate);

    return 0;
}

/* Returns a random float64 between 0 and 1 */
double
rng·random(void)
{
    uint64 r = xoshiro256ss(&RNG); 
    return (double)r / (double)UINT64_MAX;
}

double
rng·exponential(double lambda)
{
    double f;

    f = rng·random();
    return -log(1 - f)/lambda;
}

double
rng·normal(void)
{
    double f;
    f = rng·random();

}

/* Returns true or false on success of trial */
bool
rng·bernoulli(double f)
{
    return rng·random() < f;
}

/* Returns a random integer between 0 and max
 * TODO: Modulo throws off uniformity 
 */
uint64
rng·randi(int max)
{
    uint64 r = xoshiro256ss(&RNG); 
    return r % max;
}

uint64
rng·poisson(double mean)
{
    uint64 n;
    double c;

    if(mean<10.0) {
        for(n=0, c=rng·exponential(1.0); c<mean; ++n, c+=rng·exponential(1.0))
            ;
        return n;
    }

    return 0;
}