VCV Rack API v2
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1#pragma once
2#include <common.hpp>
3#include <random>
4#include <vector>
5
6
7namespace rack {
9namespace random {
10
11
27 using result_type = uint64_t;
28
29 uint64_t state[2] = {};
30
32
33 explicit Xoroshiro128Plus(uint64_t s0, uint64_t s1 = 0) {
34 seed(s0, s1);
35 }
36
37 void seed(uint64_t s0, uint64_t s1 = 0) {
38 state[0] = s0;
39 state[1] = s1;
40 // A bad seed will give a bad first result, so shift the state
41 operator()();
42 }
43
44 bool isSeeded() {
45 return state[0] || state[1];
46 }
47
48 static uint64_t rotl(uint64_t x, int k) {
49 return (x << k) | (x >> (64 - k));
50 }
51
52 uint64_t operator()() {
53 uint64_t s0 = state[0];
54 uint64_t s1 = state[1];
55 uint64_t result = s0 + s1;
56
57 s1 ^= s0;
58 state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
59 state[1] = rotl(s1, 36);
60
61 return result;
62 }
63
64 static constexpr uint64_t min() {
65 return 0;
66 }
67 static constexpr uint64_t max() {
68 return UINT64_MAX;
69 }
70};
71
72
73// Simple global API
74
75void init();
76
81
82template <typename T>
83T get() {
84 // Call operator()() and cast by default
85 return local()();
86}
87
88template <>
89inline uint32_t get() {
90 // Take top 32 bits which has better randomness properties.
91 return get<uint64_t>() >> 32;
92}
93
94template <>
95inline uint16_t get() {
96 return get<uint64_t>() >> 48;
97}
98
99template <>
100inline uint8_t get() {
101 return get<uint64_t>() >> 56;
102}
103
104template <>
105inline bool get() {
106 return get<uint64_t>() >> 63;
107}
108
109template <>
110inline float get() {
111 // The multiplier is 2f7fffff in hex. This gives maximum precision of uint32_t -> float conversion and its image is [0, 1).
112 return get<uint32_t>() * 2.32830629e-10f;
113}
114
115template <>
116inline double get() {
117 return get<uint64_t>() * 5.421010862427522e-20;
118}
119
120
122inline uint64_t u64() {return get<uint64_t>();}
124inline uint32_t u32() {return get<uint32_t>();}
126inline float uniform() {return get<float>();}
127
129inline float normal() {
130 // Box-Muller transform
131 float radius = std::sqrt(-2.f * std::log(1.f - get<float>()));
132 float theta = 2.f * M_PI * get<float>();
133 return radius * std::sin(theta);
134
135 // // Central Limit Theorem
136 // const int n = 8;
137 // float sum = 0.0;
138 // for (int i = 0; i < n; i++) {
139 // sum += get<float>();
140 // }
141 // return (sum - n / 2.f) / std::sqrt(n / 12.f);
142}
143
145inline void buffer(uint8_t* out, size_t len) {
146 Xoroshiro128Plus& rng = local();
147 for (size_t i = 0; i < len; i += 4) {
148 uint64_t r = rng();
149 out[i] = r;
150 if (i + 1 < len)
151 out[i + 1] = r >> 8;
152 if (i + 2 < len)
153 out[i + 2] = r >> 16;
154 if (i + 3 < len)
155 out[i + 3] = r >> 24;
156 }
157}
158
160inline std::vector<uint8_t> vector(size_t len) {
161 std::vector<uint8_t> v(len);
162 buffer(v.data(), len);
163 return v;
164}
165
166
167} // namespace random
168} // namespace rack
Random number generation.
Definition random.hpp:9
float normal()
Returns a normal random number with mean 0 and standard deviation 1.
Definition random.hpp:129
void buffer(uint8_t *out, size_t len)
Fills an array with random bytes.
Definition random.hpp:145
Xoroshiro128Plus & local()
Returns the generator.
uint64_t u64()
Returns a uniform random uint64_t from 0 to UINT64_MAX.
Definition random.hpp:122
float uniform()
Returns a uniform random float in the interval [0.0, 1.0).
Definition random.hpp:126
std::vector< uint8_t > vector(size_t len)
Creates a vector of random bytes.
Definition random.hpp:160
T get()
Definition random.hpp:83
uint32_t u32()
Returns a uniform random uint32_t from 0 to UINT32_MAX.
Definition random.hpp:124
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
xoroshiro128+.
Definition random.hpp:26
Xoroshiro128Plus()
Definition random.hpp:31
static constexpr uint64_t min()
Definition random.hpp:64
void seed(uint64_t s0, uint64_t s1=0)
Definition random.hpp:37
Xoroshiro128Plus(uint64_t s0, uint64_t s1=0)
Definition random.hpp:33
static uint64_t rotl(uint64_t x, int k)
Definition random.hpp:48
uint64_t operator()()
Definition random.hpp:52
uint64_t result_type
Definition random.hpp:27
static constexpr uint64_t max()
Definition random.hpp:67
bool isSeeded()
Definition random.hpp:44
uint64_t state[2]
Definition random.hpp:29