aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Sunet <msunet@shellblade.net>2022-05-07 08:09:10 -0700
committerMarc Sunet <msunet@shellblade.net>2022-05-07 08:09:10 -0700
commit344960f7470d7b6e5cb0c0b1d7e751d47063a98c (patch)
tree138d163f10f8f5c5bb8d5624d5914bc7506da33c
parente504a003cf5e88c9440ab49e10792e0e26b96c45 (diff)
Add random library.
-rw-r--r--CMakeLists.txt1
-rw-r--r--random/CMakeLists.txt10
-rw-r--r--random/LICENSE60
-rw-r--r--random/include/random/mt19937-64.h40
-rw-r--r--random/include/random/random.h3
-rw-r--r--random/src/mt19937-64.c183
6 files changed, 297 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8eb368e..4695e06 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,4 +7,5 @@ add_subdirectory(list)
7add_subdirectory(listpool) 7add_subdirectory(listpool)
8add_subdirectory(log) 8add_subdirectory(log)
9add_subdirectory(mempool) 9add_subdirectory(mempool)
10add_subdirectory(random)
10add_subdirectory(timer) 11add_subdirectory(timer)
diff --git a/random/CMakeLists.txt b/random/CMakeLists.txt
new file mode 100644
index 0000000..ec80b1d
--- /dev/null
+++ b/random/CMakeLists.txt
@@ -0,0 +1,10 @@
1cmake_minimum_required(VERSION 3.0)
2
3project(random)
4
5add_library(random
6 src/mt19937-64.c)
7
8target_include_directories(random PUBLIC include)
9
10target_compile_options(random PRIVATE -Wall -Wextra)
diff --git a/random/LICENSE b/random/LICENSE
new file mode 100644
index 0000000..867af1f
--- /dev/null
+++ b/random/LICENSE
@@ -0,0 +1,60 @@
1This library is licensed under the GNU AFFERO GENERAL PUBLIC LICENSE. The
2license text can be found in the top-level LICENSE file of this project.
3
4This library makes use of third-party software. The licenses for this
5third-party software are listed below.
6
7# mt19937-64.c
8
9A C-program for MT19937-64 (2004/9/29 version).
10Coded by Takuji Nishimura and Makoto Matsumoto.
11
12This is a 64-bit version of Mersenne Twister pseudorandom number
13generator.
14
15Before using, initialize the state by using init_genrand64(seed)
16or init_by_array64(init_key, key_length).
17
18Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
19All rights reserved.
20
21Redistribution and use in source and binary forms, with or without
22modification, are permitted provided that the following conditions
23are met:
24
251. Redistributions of source code must retain the above copyright
26notice, this list of conditions and the following disclaimer.
27
282. Redistributions in binary form must reproduce the above copyright
29notice, this list of conditions and the following disclaimer in the
30documentation and/or other materials provided with the distribution.
31
323. The names of its contributors may not be used to endorse or promote
33products derived from this software without specific prior written
34permission.
35
36THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
40CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
41EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
48References:
49T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
50ACM Transactions on Modeling and
51Computer Simulation 10. (2000) 348--357.
52M. Matsumoto and T. Nishimura,
53``Mersenne Twister: a 623-dimensionally equidistributed
54 uniform pseudorandom number generator''
55ACM Transactions on Modeling and
56Computer Simulation 8. (Jan. 1998) 3--30.
57
58Any feedback is very welcome.
59http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
60email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
diff --git a/random/include/random/mt19937-64.h b/random/include/random/mt19937-64.h
new file mode 100644
index 0000000..bd1125f
--- /dev/null
+++ b/random/include/random/mt19937-64.h
@@ -0,0 +1,40 @@
1#pragma once
2
3#include <stdint.h>
4
5#define MT19937_64_NN 312
6
7typedef struct mt19937_64 {
8 uint64_t mt[MT19937_64_NN]; // The array for the state vector.
9 int mti; // mti==NN+1 means mt[NN] is not initialized.
10} mt19937_64;
11
12/// Creates a default-initialized 64-bit Mersenne Twister PRNG.
13mt19937_64 mt19937_64_make();
14
15/// Initializes the PRNG with a seed.
16void mt19937_64_init(mt19937_64* rng, uint64_t seed);
17
18/// Initializes the PRNG with an array of values.
19/// |init_key| is the array for initializing keys.
20/// |key_length| is its length.
21void mt19937_64_init_by_array64(
22 mt19937_64* rng, uint64_t init_key[], uint64_t key_length);
23
24/// Generates a random number in the [0, 2^64-1]-interval.
25uint64_t mt19937_64_gen64u(mt19937_64* rng);
26
27/// Generates a random number in the [0, 2^63-1]-interval.
28int64_t mt19937_64_gen64i(mt19937_64* rng);
29
30/// Generates a random number in the [0,1]-real-interval.
31double mt19937_64_gen_real1(mt19937_64* rng);
32
33/// Generates a random number in the [0,1)-real-interval.
34double mt19937_64_gen_real2(mt19937_64* rng);
35
36/// Generates a random number in the (0,1)-real-interval.
37double mt19937_64_gen_real3(mt19937_64* rng);
38
39/// Generates a random number in the (-1,+1)-real-interval.
40double mt19937_64_gen_real4(mt19937_64* rng);
diff --git a/random/include/random/random.h b/random/include/random/random.h
new file mode 100644
index 0000000..5499f62
--- /dev/null
+++ b/random/include/random/random.h
@@ -0,0 +1,3 @@
1#pragma once
2
3#include <random/mt19937-64.h>
diff --git a/random/src/mt19937-64.c b/random/src/mt19937-64.c
new file mode 100644
index 0000000..b6f7be6
--- /dev/null
+++ b/random/src/mt19937-64.c
@@ -0,0 +1,183 @@
1// http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/VERSIONS/C-LANG/mt19937-64.c
2
3/*
4 A C-program for MT19937-64 (2004/9/29 version).
5 Coded by Takuji Nishimura and Makoto Matsumoto.
6
7 This is a 64-bit version of Mersenne Twister pseudorandom number
8 generator.
9
10 Before using, initialize the state by using init_genrand64(seed)
11 or init_by_array64(init_key, key_length).
12
13 Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
19
20 1. Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
22
23 2. Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
26
27 3. The names of its contributors may not be used to endorse or promote
28 products derived from this software without specific prior written
29 permission.
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 References:
44 T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
45 ACM Transactions on Modeling and
46 Computer Simulation 10. (2000) 348--357.
47 M. Matsumoto and T. Nishimura,
48 ``Mersenne Twister: a 623-dimensionally equidistributed
49 uniform pseudorandom number generator''
50 ACM Transactions on Modeling and
51 Computer Simulation 8. (Jan. 1998) 3--30.
52
53 Any feedback is very welcome.
54 http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
55 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
56*/
57
58/*
59Modifications:
60
61- Remove stdio.h and the main() function.
62- Replace 'unsigned long long' with 'uint64_t' (include stdint.h).
63- Remove the global state and instead store the generator's state in the new
64 mt19937_64 struct.
65- Naming and formatting.
66*/
67
68#include <random/mt19937-64.h>
69
70#include <assert.h>
71
72#define NN 312
73#define MM 156
74#define MATRIX_A 0xB5026F5AA96619E9ULL
75#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
76#define LM 0x7FFFFFFFULL /* Least significant 31 bits */
77
78mt19937_64 mt19937_64_make() {
79 mt19937_64 rng;
80 rng.mti = NN+1; // Not initialized.
81 return rng;
82}
83
84/* initializes mt[NN] with a seed */
85void mt19937_64_init(mt19937_64* rng, uint64_t seed) {
86 assert(rng);
87 rng->mt[0] = seed;
88 for (rng->mti = 1; rng->mti < NN; rng->mti++) {
89 rng->mt[rng->mti] =
90 (6364136223846793005ULL * (rng->mt[rng->mti - 1] ^ (rng->mt[rng->mti - 1] >> 62)) + rng->mti);
91 }
92}
93
94/* initialize by an array with array-length */
95/* init_key is the array for initializing keys */
96/* key_length is its length */
97void mt19937_64_init_by_array64(mt19937_64* rng, uint64_t init_key[], uint64_t key_length) {
98 assert(rng);
99 uint64_t i, j, k;
100 mt19937_64_init(rng, 19650218ULL);
101 i=1; j=0;
102 k = (NN>key_length ? NN : key_length);
103 for (; k; k--) {
104 rng->mt[i] = (rng->mt[i] ^ ((rng->mt[i-1] ^ (rng->mt[i-1] >> 62)) * 3935559000370003845ULL))
105 + init_key[j] + j; /* non linear */
106 i++; j++;
107 if (i>=NN) { rng->mt[0] = rng->mt[NN-1]; i=1; }
108 if (j>=key_length) j=0;
109 }
110 for (k=NN-1; k; k--) {
111 rng->mt[i] = (rng->mt[i] ^ ((rng->mt[i-1] ^ (rng->mt[i-1] >> 62)) * 2862933555777941757ULL))
112 - i; /* non linear */
113 i++;
114 if (i>=NN) { rng->mt[0] = rng->mt[NN-1]; i=1; }
115 }
116
117 rng->mt[0] = 1ULL << 63; /* MSB is 1; assuring non-zero initial array */
118}
119
120/* generates a random number on [0, 2^64-1]-interval */
121uint64_t mt19937_64_gen64u(mt19937_64* rng) {
122 assert(rng);
123 int i;
124 uint64_t x;
125 static uint64_t mag01[2]={0ULL, MATRIX_A};
126
127 if (rng->mti >= NN) { /* generate NN words at one time */
128
129 /* if mt19937_64_init() has not been called, */
130 /* a default initial seed is used */
131 if (rng->mti == NN+1)
132 mt19937_64_init(rng, 5489ULL);
133
134 for (i=0;i<NN-MM;i++) {
135 x = (rng->mt[i]&UM)|(rng->mt[i+1]&LM);
136 rng->mt[i] = rng->mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
137 }
138 for (;i<NN-1;i++) {
139 x = (rng->mt[i]&UM)|(rng->mt[i+1]&LM);
140 rng->mt[i] = rng->mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
141 }
142 x = (rng->mt[NN-1]&UM)|(rng->mt[0]&LM);
143 rng->mt[NN-1] = rng->mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
144
145 rng->mti = 0;
146 }
147
148 x = rng->mt[rng->mti++];
149
150 x ^= (x >> 29) & 0x5555555555555555ULL;
151 x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
152 x ^= (x << 37) & 0xFFF7EEE000000000ULL;
153 x ^= (x >> 43);
154
155 return x;
156}
157
158/* generates a random number on [0, 2^63-1]-interval */
159int64_t mt19937_64_gen64i(mt19937_64* rng) {
160 return (int64_t)(mt19937_64_gen64u(rng) >> 1);
161}
162
163/* generates a random number on [0,1]-real-interval */
164double mt19937_64_gen_real1(mt19937_64* rng) {
165 return (mt19937_64_gen64u(rng) >> 11) * (1.0/9007199254740991.0);
166}
167
168/* generates a random number on [0,1)-real-interval */
169double mt19937_64_gen_real2(mt19937_64* rng) {
170 return (mt19937_64_gen64u(rng) >> 11) * (1.0/9007199254740992.0);
171}
172
173/* generates a random number on (0,1)-real-interval */
174double mt19937_64_gen_real3(mt19937_64* rng) {
175 return ((mt19937_64_gen64u(rng) >> 12) + 0.5) * (1.0/4503599627370496.0);
176}
177
178/* generates a random number on (-1,+1)-real-interval */
179double mt19937_64_gen_real4(mt19937_64* rng) {
180 const double x01 = mt19937_64_gen_real3(rng); // (0,1) interval.
181 const double x11 = x01 * 2. - 1.; // (-1, +1) interval.
182 return x11;
183}