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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/*
Copyright (c) 2013-2017 Gustav Hartvigsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
Adaptation of
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/mt19937-64.c
with the following license:
Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Random.h"
#include "primes.h"
#include <time.h>
#define NN 312
#define MM 156
#define MATRIX_A 0xB5026F5AA96619E9ULL
#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
#define LM 0x7FFFFFFFULL /* Least significant 31 bits */
struct SRandom {
sulong seed;
sulong current;
size_t index;
size_t itt;
sulong * list;
};
void
_s_internal_random_init (SRandom * self);
SRandom *
s_random_new () {
return s_random_new_with_seed ((slong) time (NULL));
}
SRandom *
s_random_new_with_seed (sulong seed) {
SRandom * self = s_malloc (sizeof (SRandom));
self->seed = seed;
self->list = s_calloc(NN, sizeof (sulong));
self->itt = NN+1;
return self;
}
void
s_random_reseed (SRandom * self, sulong seed) {
/* We just have to re-set these things. */
self->seed = seed;
self->itt = NN+1;
}
sulong
s_random_next (SRandom * self) {
size_t i;
slong x;
static slong mag01[2]={0ULL, MATRIX_A};
if (self->itt >= NN) {
if (self->itt == NN+1) {
_s_internal_random_init (self);
}
for (i = 0; i < NN - MM; i++) {
x = (self->list[i] & UM) | (self->list[i+1] & LM);
self->list[i] = self->list[i + MM] ^
(x >> 1) ^
mag01[(sint)(x & 1ULL)];
}
for (/* i */; i < NN - 1; i++) {
x = (self->list[i] & UM) | (self->list[i + 1] & LM);
self->list[i] = self->list[i + (MM - NN)] ^
(x >> 1) ^
mag01[(sint)(x & 1ULL)];
}
x = (self->list[NN - 1] & UM) | (self->list[0] & LM);
self->list[NN - 1] = self->list[MM - 1] ^
(x >> 1) ^
mag01[(sint) (x & 1ULL)];
self->itt = 0;
}
self->current = self->list[self->itt];
x = self->list[self->itt++];
x ^= (x >> 29) & 0x5555555555555555ULL;
x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
x ^= (x << 37) & 0xFFF7EEE000000000ULL;
x ^= (x >> 43);
return x;
}
sulong
s_random_current (SRandom * self) {
return self->current;
}
slong
s_random_next_long (SRandom * self) {
return (s_random_next (self) >> 1);
}
sdouble
s_random_next_real (SRandom * self) {
return (s_random_next (self) >> 11) * (1.0/9007199254740991.0);
}
slong
s_random_current_long (SRandom * self) {
sulong current = self->current;
return (current >> 1);
}
sdouble
s_random_current_real (SRandom * self) {
sulong current = self->current;
return (current >> 11) * (1.0/9007199254740991.0);
}
void
_s_internal_random_init (SRandom * self) {
self->list[0] = self->seed;
for (self->itt = 0; self->itt < NN; self->itt++) {
self->list[self->itt] =
(6364136223846793005ULL * (self->list[self->itt - 1] ^
(self->list[self->itt - 1] >> 62)) + self->itt);
}
}
#if 0
slong
s_random_get_entropy () {
#ifdef _WIN32
#elif __unix__
#else
#error "We can't get at an entropy pool?"
#endif
}
#endif
|