2
Copyright (c) 2013-2017 Gustav Hartvigsson
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/mt19937-64.c
26
with the following license:
28
Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
31
Redistribution and use in source and binary forms, with or without
32
modification, are permitted provided that the following conditions
35
1. Redistributions of source code must retain the above copyright
36
notice, this list of conditions and the following disclaimer.
38
2. Redistributions in binary form must reproduce the above copyright
39
notice, this list of conditions and the following disclaimer in the
40
documentation and/or other materials provided with the distribution.
42
3. The names of its contributors may not be used to endorse or promote
43
products derived from this software without specific prior written
46
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
49
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
50
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
52
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
53
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
54
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
56
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66
#define MATRIX_A 0xB5026F5AA96619E9ULL
67
#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
68
#define LM 0x7FFFFFFFULL /* Least significant 31 bits */
81
_s_internal_random_init (SRandom * self);
85
return s_random_new_with_seed ((slong) time (NULL));
89
s_random_new_with_seed (sulong seed) {
90
SRandom * self = s_malloc (sizeof (SRandom));
94
self->list = s_calloc(NN, sizeof (sulong));
102
s_random_reseed (SRandom * self, sulong seed) {
103
/* We just have to re-set these things. */
109
s_random_next (SRandom * self) {
113
static slong mag01[2]={0ULL, MATRIX_A};
115
if (self->itt >= NN) {
116
if (self->itt == NN+1) {
117
_s_internal_random_init (self);
120
for (i = 0; i < NN - MM; i++) {
121
x = (self->list[i] & UM) | (self->list[i+1] & LM);
122
self->list[i] = self->list[i + MM] ^
124
mag01[(sint)(x & 1ULL)];
127
for (/* i */; i < NN - 1; i++) {
128
x = (self->list[i] & UM) | (self->list[i + 1] & LM);
129
self->list[i] = self->list[i + (MM - NN)] ^
131
mag01[(sint)(x & 1ULL)];
134
x = (self->list[NN - 1] & UM) | (self->list[0] & LM);
136
self->list[NN - 1] = self->list[MM - 1] ^
138
mag01[(sint) (x & 1ULL)];
143
self->current = self->list[self->itt];
145
x = self->list[self->itt++];
147
x ^= (x >> 29) & 0x5555555555555555ULL;
148
x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
149
x ^= (x << 37) & 0xFFF7EEE000000000ULL;
156
s_random_current (SRandom * self) {
157
return self->current;
161
s_random_next_long (SRandom * self) {
162
return (s_random_next (self) >> 1);
166
s_random_next_real (SRandom * self) {
167
return (s_random_next (self) >> 11) * (1.0/9007199254740991.0);
171
s_random_current_long (SRandom * self) {
172
sulong current = self->current;
173
return (current >> 1);
177
s_random_current_real (SRandom * self) {
178
sulong current = self->current;
179
return (current >> 11) * (1.0/9007199254740991.0);
183
_s_internal_random_init (SRandom * self) {
184
self->list[0] = self->seed;
186
for (self->itt = 0; self->itt < NN; self->itt++) {
187
self->list[self->itt] =
188
(6364136223846793005ULL * (self->list[self->itt - 1] ^
189
(self->list[self->itt - 1] >> 62)) + self->itt);
195
s_random_get_entropy () {
201
#error "We can't get at an entropy pool?"