deba@2229
|
1 |
/* -*- C++ -*-
|
deba@2229
|
2 |
*
|
deba@2229
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
deba@2229
|
4 |
*
|
deba@2229
|
5 |
* Copyright (C) 2003-2006
|
deba@2229
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@2229
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@2229
|
8 |
*
|
deba@2229
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@2229
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@2229
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@2229
|
12 |
*
|
deba@2229
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@2229
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@2229
|
15 |
* purpose.
|
deba@2229
|
16 |
*
|
deba@2229
|
17 |
*/
|
deba@2229
|
18 |
|
deba@2229
|
19 |
#ifndef LEMON_RANDOM_H
|
deba@2229
|
20 |
#define LEMON_RANDOM_H
|
deba@2229
|
21 |
|
deba@2229
|
22 |
#include <algorithm>
|
deba@2242
|
23 |
#include <iterator>
|
deba@2242
|
24 |
#include <vector>
|
deba@2229
|
25 |
|
deba@2229
|
26 |
#include <ctime>
|
deba@2229
|
27 |
#include <cmath>
|
deba@2229
|
28 |
|
deba@2229
|
29 |
///\ingroup misc
|
deba@2229
|
30 |
///\file
|
deba@2229
|
31 |
///\brief Mersenne Twister random number generator
|
deba@2229
|
32 |
///
|
deba@2229
|
33 |
///\author Balazs Dezso
|
deba@2229
|
34 |
|
deba@2229
|
35 |
namespace lemon {
|
deba@2229
|
36 |
|
deba@2242
|
37 |
namespace _random_bits {
|
deba@2242
|
38 |
|
deba@2242
|
39 |
template <typename _Word, int _bits = std::numeric_limits<_Word>::digits>
|
deba@2242
|
40 |
struct RandomTraits {};
|
deba@2242
|
41 |
|
deba@2242
|
42 |
template <typename _Word>
|
deba@2242
|
43 |
struct RandomTraits<_Word, 32> {
|
deba@2242
|
44 |
|
deba@2242
|
45 |
typedef _Word Word;
|
deba@2242
|
46 |
static const int bits = 32;
|
deba@2242
|
47 |
|
deba@2242
|
48 |
static const int length = 624;
|
deba@2242
|
49 |
static const int shift = 397;
|
deba@2242
|
50 |
|
deba@2242
|
51 |
static const Word mul = 0x6c078965u;
|
deba@2242
|
52 |
static const Word arrayInit = 0x012BD6AAu;
|
deba@2242
|
53 |
static const Word arrayMul1 = 0x0019660Du;
|
deba@2242
|
54 |
static const Word arrayMul2 = 0x5D588B65u;
|
deba@2242
|
55 |
|
deba@2242
|
56 |
static const Word mask = 0x9908B0DFu;
|
deba@2242
|
57 |
static const Word loMask = (1u << 31) - 1;
|
deba@2242
|
58 |
static const Word hiMask = ~loMask;
|
deba@2242
|
59 |
|
deba@2242
|
60 |
|
deba@2242
|
61 |
static Word tempering(Word rnd) {
|
deba@2242
|
62 |
rnd ^= (rnd >> 11);
|
deba@2242
|
63 |
rnd ^= (rnd << 7) & 0x9D2C5680u;
|
deba@2242
|
64 |
rnd ^= (rnd << 15) & 0xEFC60000u;
|
deba@2242
|
65 |
rnd ^= (rnd >> 18);
|
deba@2242
|
66 |
return rnd;
|
deba@2242
|
67 |
}
|
deba@2242
|
68 |
|
deba@2242
|
69 |
};
|
deba@2242
|
70 |
|
deba@2242
|
71 |
template <typename _Word>
|
deba@2242
|
72 |
struct RandomTraits<_Word, 64> {
|
deba@2242
|
73 |
|
deba@2242
|
74 |
typedef _Word Word;
|
deba@2242
|
75 |
static const int bits = 64;
|
deba@2242
|
76 |
|
deba@2242
|
77 |
static const int length = 312;
|
deba@2242
|
78 |
static const int shift = 156;
|
deba@2242
|
79 |
|
deba@2242
|
80 |
static const Word mul = (Word)0x5851F42Du << 32 | (Word)0x4C957F2Du;
|
deba@2242
|
81 |
static const Word arrayInit = (Word)0x00000000u << 32 |(Word)0x012BD6AAu;
|
deba@2242
|
82 |
static const Word arrayMul1 = (Word)0x369DEA0Fu << 32 |(Word)0x31A53F85u;
|
deba@2242
|
83 |
static const Word arrayMul2 = (Word)0x27BB2EE6u << 32 |(Word)0x87B0B0FDu;
|
deba@2242
|
84 |
|
deba@2242
|
85 |
static const Word mask = (Word)0xB5026F5Au << 32 | (Word)0xA96619E9u;
|
deba@2242
|
86 |
static const Word loMask = ((Word)1u << 31) - 1;
|
deba@2242
|
87 |
static const Word hiMask = ~loMask;
|
deba@2242
|
88 |
|
deba@2242
|
89 |
static Word tempering(Word rnd) {
|
deba@2242
|
90 |
rnd ^= (rnd >> 29) & ((Word)0x55555555u << 32 | (Word)0x55555555u);
|
deba@2242
|
91 |
rnd ^= (rnd << 17) & ((Word)0x71D67FFFu << 32 | (Word)0xEDA60000u);
|
deba@2242
|
92 |
rnd ^= (rnd << 37) & ((Word)0xFFF7EEE0u << 32 | (Word)0x00000000u);
|
deba@2242
|
93 |
rnd ^= (rnd >> 43);
|
deba@2242
|
94 |
return rnd;
|
deba@2242
|
95 |
}
|
deba@2242
|
96 |
|
deba@2242
|
97 |
};
|
deba@2242
|
98 |
|
deba@2242
|
99 |
template <typename _Word>
|
deba@2242
|
100 |
class RandomCore {
|
deba@2242
|
101 |
public:
|
deba@2242
|
102 |
|
deba@2242
|
103 |
typedef _Word Word;
|
deba@2242
|
104 |
|
deba@2242
|
105 |
private:
|
deba@2242
|
106 |
|
deba@2242
|
107 |
static const int bits = RandomTraits<Word>::bits;
|
deba@2242
|
108 |
|
deba@2242
|
109 |
static const int length = RandomTraits<Word>::length;
|
deba@2242
|
110 |
static const int shift = RandomTraits<Word>::shift;
|
deba@2242
|
111 |
|
deba@2242
|
112 |
public:
|
deba@2242
|
113 |
|
deba@2242
|
114 |
void initState() {
|
deba@2242
|
115 |
static const Word seedArray[4] = {
|
deba@2242
|
116 |
0x12345u, 0x23456u, 0x34567u, 0x45678u
|
deba@2242
|
117 |
};
|
deba@2242
|
118 |
|
deba@2242
|
119 |
initState(seedArray, seedArray + 4);
|
deba@2242
|
120 |
}
|
deba@2242
|
121 |
|
deba@2242
|
122 |
void initState(Word seed) {
|
deba@2242
|
123 |
|
deba@2242
|
124 |
static const Word mul = RandomTraits<Word>::mul;
|
deba@2242
|
125 |
|
deba@2242
|
126 |
current = state;
|
deba@2242
|
127 |
|
deba@2242
|
128 |
Word *curr = state + length - 1;
|
deba@2242
|
129 |
curr[0] = seed; --curr;
|
deba@2242
|
130 |
for (int i = 1; i < length; ++i) {
|
deba@2242
|
131 |
curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i);
|
deba@2242
|
132 |
--curr;
|
deba@2242
|
133 |
}
|
deba@2242
|
134 |
}
|
deba@2242
|
135 |
|
deba@2242
|
136 |
template <typename Iterator>
|
deba@2242
|
137 |
void initState(Iterator begin, Iterator end) {
|
deba@2242
|
138 |
|
deba@2242
|
139 |
static const Word init = RandomTraits<Word>::arrayInit;
|
deba@2242
|
140 |
static const Word mul1 = RandomTraits<Word>::arrayMul1;
|
deba@2242
|
141 |
static const Word mul2 = RandomTraits<Word>::arrayMul2;
|
deba@2242
|
142 |
|
deba@2242
|
143 |
|
deba@2242
|
144 |
Word *curr = state + length - 1; --curr;
|
deba@2242
|
145 |
Iterator it = begin; int cnt = 0;
|
deba@2242
|
146 |
int num;
|
deba@2242
|
147 |
|
deba@2242
|
148 |
initState(init);
|
deba@2242
|
149 |
|
deba@2242
|
150 |
num = length > end - begin ? length : end - begin;
|
deba@2242
|
151 |
while (num--) {
|
deba@2242
|
152 |
curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1))
|
deba@2242
|
153 |
+ *it + cnt;
|
deba@2242
|
154 |
++it; ++cnt;
|
deba@2242
|
155 |
if (it == end) {
|
deba@2242
|
156 |
it = begin; cnt = 0;
|
deba@2242
|
157 |
}
|
deba@2242
|
158 |
if (curr == state) {
|
deba@2242
|
159 |
curr = state + length - 1; curr[0] = state[0];
|
deba@2242
|
160 |
}
|
deba@2242
|
161 |
--curr;
|
deba@2242
|
162 |
}
|
deba@2242
|
163 |
|
deba@2242
|
164 |
num = length - 1; cnt = length - (curr - state) - 1;
|
deba@2242
|
165 |
while (num--) {
|
deba@2242
|
166 |
curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2))
|
deba@2242
|
167 |
- cnt;
|
deba@2242
|
168 |
--curr; ++cnt;
|
deba@2242
|
169 |
if (curr == state) {
|
deba@2242
|
170 |
curr = state + length - 1; curr[0] = state[0]; --curr;
|
deba@2242
|
171 |
cnt = 1;
|
deba@2242
|
172 |
}
|
deba@2242
|
173 |
}
|
deba@2242
|
174 |
|
deba@2242
|
175 |
state[length - 1] = (Word)1 << (bits - 1);
|
deba@2242
|
176 |
}
|
deba@2242
|
177 |
|
deba@2242
|
178 |
void copyState(const RandomCore& other) {
|
deba@2242
|
179 |
std::copy(other.state, other.state + length, state);
|
deba@2242
|
180 |
current = state + (other.current - other.state);
|
deba@2242
|
181 |
}
|
deba@2242
|
182 |
|
deba@2242
|
183 |
Word operator()() {
|
deba@2242
|
184 |
if (current == state) fillState();
|
deba@2242
|
185 |
--current;
|
deba@2242
|
186 |
Word rnd = *current;
|
deba@2242
|
187 |
return RandomTraits<Word>::tempering(rnd);
|
deba@2242
|
188 |
}
|
deba@2242
|
189 |
|
deba@2242
|
190 |
private:
|
deba@2242
|
191 |
|
deba@2242
|
192 |
|
deba@2242
|
193 |
void fillState() {
|
deba@2242
|
194 |
static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
|
deba@2242
|
195 |
static const Word loMask = RandomTraits<Word>::loMask;
|
deba@2242
|
196 |
static const Word hiMask = RandomTraits<Word>::hiMask;
|
deba@2242
|
197 |
|
deba@2242
|
198 |
current = state + length;
|
deba@2242
|
199 |
|
deba@2242
|
200 |
register Word *curr = state + length - 1;
|
deba@2242
|
201 |
register long num;
|
deba@2242
|
202 |
|
deba@2242
|
203 |
num = length - shift;
|
deba@2242
|
204 |
while (num--) {
|
deba@2242
|
205 |
curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
|
deba@2242
|
206 |
curr[- shift] ^ mask[curr[-1] & 1ul];
|
deba@2242
|
207 |
--curr;
|
deba@2242
|
208 |
}
|
deba@2242
|
209 |
num = shift - 1;
|
deba@2242
|
210 |
while (num--) {
|
deba@2242
|
211 |
curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
|
deba@2242
|
212 |
curr[length - shift] ^ mask[curr[-1] & 1ul];
|
deba@2242
|
213 |
--curr;
|
deba@2242
|
214 |
}
|
deba@2242
|
215 |
curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
|
deba@2242
|
216 |
curr[length - shift] ^ mask[curr[length - 1] & 1ul];
|
deba@2242
|
217 |
|
deba@2242
|
218 |
}
|
deba@2242
|
219 |
|
deba@2242
|
220 |
|
deba@2242
|
221 |
Word *current;
|
deba@2242
|
222 |
Word state[length];
|
deba@2242
|
223 |
|
deba@2242
|
224 |
};
|
deba@2242
|
225 |
|
deba@2242
|
226 |
|
deba@2242
|
227 |
template <typename Result,
|
deba@2242
|
228 |
int shift = (std::numeric_limits<Result>::digits + 1) / 2>
|
deba@2242
|
229 |
struct Masker {
|
deba@2242
|
230 |
static Result mask(const Result& result) {
|
deba@2242
|
231 |
return Masker<Result, (shift + 1) / 2>::
|
deba@2242
|
232 |
mask((Result)(result | (result >> shift)));
|
deba@2242
|
233 |
}
|
deba@2242
|
234 |
};
|
deba@2242
|
235 |
|
deba@2242
|
236 |
template <typename Result>
|
deba@2242
|
237 |
struct Masker<Result, 1> {
|
deba@2242
|
238 |
static Result mask(const Result& result) {
|
deba@2242
|
239 |
return (Result)(result | (result >> 1));
|
deba@2242
|
240 |
}
|
deba@2242
|
241 |
};
|
deba@2242
|
242 |
|
deba@2242
|
243 |
template <typename Result, typename Word,
|
deba@2242
|
244 |
int rest = std::numeric_limits<Result>::digits, int shift = 0,
|
deba@2242
|
245 |
bool last = rest <= std::numeric_limits<Word>::digits>
|
deba@2242
|
246 |
struct IntConversion {
|
deba@2242
|
247 |
static const int bits = std::numeric_limits<Word>::digits;
|
deba@2242
|
248 |
|
deba@2242
|
249 |
static Result convert(RandomCore<Word>& rnd) {
|
deba@2242
|
250 |
return (Result)(rnd() >> (bits - rest)) << shift;
|
deba@2242
|
251 |
}
|
deba@2242
|
252 |
|
deba@2242
|
253 |
};
|
deba@2242
|
254 |
|
deba@2242
|
255 |
template <typename Result, typename Word, int rest, int shift>
|
deba@2242
|
256 |
struct IntConversion<Result, Word, rest, shift, false> {
|
deba@2242
|
257 |
static const int bits = std::numeric_limits<Word>::digits;
|
deba@2242
|
258 |
|
deba@2242
|
259 |
static Result convert(RandomCore<Word>& rnd) {
|
deba@2242
|
260 |
return ((Result)rnd() << shift) |
|
deba@2242
|
261 |
IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd);
|
deba@2242
|
262 |
}
|
deba@2242
|
263 |
};
|
deba@2242
|
264 |
|
deba@2242
|
265 |
|
deba@2242
|
266 |
template <typename Result, typename Word,
|
deba@2242
|
267 |
bool one_word = std::numeric_limits<Word>::digits <
|
deba@2242
|
268 |
std::numeric_limits<Result>::digits>
|
deba@2242
|
269 |
struct Mapping {
|
deba@2242
|
270 |
static Result map(RandomCore<Word>& rnd, const Result& bound) {
|
deba@2242
|
271 |
Word max = (Word)(bound - 1);
|
deba@2242
|
272 |
Result mask = Masker<Result>::mask(bound - 1);
|
deba@2242
|
273 |
Result num;
|
deba@2242
|
274 |
do {
|
deba@2242
|
275 |
num = IntConversion<Result, Word>::convert(rnd) & mask;
|
deba@2242
|
276 |
} while (num > max);
|
deba@2242
|
277 |
return num;
|
deba@2242
|
278 |
}
|
deba@2242
|
279 |
};
|
deba@2242
|
280 |
|
deba@2242
|
281 |
template <typename Result, typename Word>
|
deba@2242
|
282 |
struct Mapping<Result, Word, false> {
|
deba@2242
|
283 |
static Result map(RandomCore<Word>& rnd, const Result& bound) {
|
deba@2242
|
284 |
Word max = (Word)(bound - 1);
|
deba@2242
|
285 |
Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2>
|
deba@2242
|
286 |
::mask(max);
|
deba@2242
|
287 |
Word num;
|
deba@2242
|
288 |
do {
|
deba@2242
|
289 |
num = rnd() & mask;
|
deba@2242
|
290 |
} while (num > max);
|
deba@2242
|
291 |
return num;
|
deba@2242
|
292 |
}
|
deba@2242
|
293 |
};
|
deba@2242
|
294 |
|
deba@2242
|
295 |
template <typename Result, int exp, bool pos = (exp >= 0)>
|
deba@2242
|
296 |
struct ShiftMultiplier {
|
deba@2242
|
297 |
static const Result multiplier() {
|
deba@2242
|
298 |
Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
|
deba@2242
|
299 |
res *= res;
|
deba@2242
|
300 |
if ((exp & 1) == 1) res *= (Result)2.0;
|
deba@2242
|
301 |
return res;
|
deba@2242
|
302 |
}
|
deba@2242
|
303 |
};
|
deba@2242
|
304 |
|
deba@2242
|
305 |
template <typename Result, int exp>
|
deba@2242
|
306 |
struct ShiftMultiplier<Result, exp, false> {
|
deba@2242
|
307 |
static const Result multiplier() {
|
deba@2242
|
308 |
Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
|
deba@2242
|
309 |
res *= res;
|
deba@2242
|
310 |
if ((exp & 1) == 1) res *= (Result)0.5;
|
deba@2242
|
311 |
return res;
|
deba@2242
|
312 |
}
|
deba@2242
|
313 |
};
|
deba@2242
|
314 |
|
deba@2242
|
315 |
template <typename Result>
|
deba@2242
|
316 |
struct ShiftMultiplier<Result, 0, true> {
|
deba@2242
|
317 |
static const Result multiplier() {
|
deba@2242
|
318 |
return (Result)1.0;
|
deba@2242
|
319 |
}
|
deba@2242
|
320 |
};
|
deba@2242
|
321 |
|
deba@2242
|
322 |
template <typename Result>
|
deba@2242
|
323 |
struct ShiftMultiplier<Result, -20, true> {
|
deba@2242
|
324 |
static const Result multiplier() {
|
deba@2242
|
325 |
return (Result)(1.0/1048576.0);
|
deba@2242
|
326 |
}
|
deba@2242
|
327 |
};
|
deba@2242
|
328 |
|
deba@2242
|
329 |
template <typename Result>
|
deba@2242
|
330 |
struct ShiftMultiplier<Result, -32, true> {
|
deba@2242
|
331 |
static const Result multiplier() {
|
deba@2242
|
332 |
return (Result)(1.0/424967296.0);
|
deba@2242
|
333 |
}
|
deba@2242
|
334 |
};
|
deba@2242
|
335 |
|
deba@2242
|
336 |
template <typename Result>
|
deba@2242
|
337 |
struct ShiftMultiplier<Result, -53, true> {
|
deba@2242
|
338 |
static const Result multiplier() {
|
deba@2242
|
339 |
return (Result)(1.0/9007199254740992.0);
|
deba@2242
|
340 |
}
|
deba@2242
|
341 |
};
|
deba@2242
|
342 |
|
deba@2242
|
343 |
template <typename Result>
|
deba@2242
|
344 |
struct ShiftMultiplier<Result, -64, true> {
|
deba@2242
|
345 |
static const Result multiplier() {
|
deba@2242
|
346 |
return (Result)(1.0/18446744073709551616.0);
|
deba@2242
|
347 |
}
|
deba@2242
|
348 |
};
|
deba@2242
|
349 |
|
deba@2242
|
350 |
template <typename Result, int exp>
|
deba@2242
|
351 |
struct Shifting {
|
deba@2242
|
352 |
static Result shift(const Result& result) {
|
deba@2242
|
353 |
return result * ShiftMultiplier<Result, exp>::multiplier();
|
deba@2242
|
354 |
}
|
deba@2242
|
355 |
};
|
deba@2242
|
356 |
|
deba@2242
|
357 |
template <typename Result, typename Word,
|
deba@2242
|
358 |
int rest = std::numeric_limits<Result>::digits, int shift = 0,
|
deba@2242
|
359 |
bool last = rest <= std::numeric_limits<Word>::digits>
|
deba@2242
|
360 |
struct RealConversion{
|
deba@2242
|
361 |
static const int bits = std::numeric_limits<Word>::digits;
|
deba@2242
|
362 |
|
deba@2242
|
363 |
static Result convert(RandomCore<Word>& rnd) {
|
deba@2242
|
364 |
return Shifting<Result, - shift - rest>::
|
deba@2242
|
365 |
shift((Result)(rnd() >> (bits - rest)));
|
deba@2242
|
366 |
}
|
deba@2242
|
367 |
};
|
deba@2242
|
368 |
|
deba@2242
|
369 |
template <typename Result, typename Word, int rest, int shift>
|
deba@2242
|
370 |
struct RealConversion<Result, Word, rest, shift, false> {
|
deba@2242
|
371 |
static const int bits = std::numeric_limits<Word>::digits;
|
deba@2242
|
372 |
|
deba@2242
|
373 |
static Result convert(RandomCore<Word>& rnd) {
|
deba@2242
|
374 |
return Shifting<Result, - shift - bits>::shift((Result)rnd()) +
|
deba@2242
|
375 |
RealConversion<Result, Word, rest-bits, shift + bits>::convert(rnd);
|
deba@2242
|
376 |
}
|
deba@2242
|
377 |
};
|
deba@2242
|
378 |
|
deba@2242
|
379 |
template <typename Result, typename Word>
|
deba@2242
|
380 |
struct Initializer {
|
deba@2242
|
381 |
|
deba@2242
|
382 |
template <typename Iterator>
|
deba@2242
|
383 |
static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) {
|
deba@2242
|
384 |
std::vector<Word> ws;
|
deba@2242
|
385 |
for (Iterator it = begin; it != end; ++it) {
|
deba@2242
|
386 |
ws.push_back((Word)*it);
|
deba@2242
|
387 |
}
|
deba@2242
|
388 |
rnd.initState(ws.begin(), ws.end());
|
deba@2242
|
389 |
}
|
deba@2242
|
390 |
|
deba@2242
|
391 |
static void init(RandomCore<Word>& rnd, Result seed) {
|
deba@2242
|
392 |
rnd.initState(seed);
|
deba@2242
|
393 |
}
|
deba@2242
|
394 |
};
|
deba@2242
|
395 |
|
deba@2242
|
396 |
template <typename Word>
|
deba@2242
|
397 |
struct BoolConversion {
|
deba@2242
|
398 |
static bool convert(RandomCore<Word>& rnd) {
|
deba@2242
|
399 |
return (rnd() & 1) == 1;
|
deba@2242
|
400 |
}
|
deba@2242
|
401 |
};
|
deba@2242
|
402 |
|
deba@2242
|
403 |
}
|
deba@2229
|
404 |
|
deba@2229
|
405 |
/// \ingroup misc
|
deba@2229
|
406 |
///
|
deba@2229
|
407 |
/// \brief Mersenne Twister random number generator
|
deba@2229
|
408 |
///
|
deba@2229
|
409 |
/// The Mersenne Twister is a twisted generalized feedback
|
deba@2242
|
410 |
/// shift-register generator of Matsumoto and Nishimura. The period
|
deba@2242
|
411 |
/// of this generator is \f$ 2^{19937} - 1 \f$ and it is
|
deba@2242
|
412 |
/// equi-distributed in 623 dimensions for 32-bit numbers. The time
|
deba@2242
|
413 |
/// performance of this generator is comparable to the commonly used
|
deba@2242
|
414 |
/// generators.
|
deba@2242
|
415 |
///
|
deba@2242
|
416 |
/// This implementation is specialized for both 32-bit and 64-bit
|
deba@2242
|
417 |
/// architectures. The generators differ sligthly in the
|
deba@2242
|
418 |
/// initialization and generation phase so they produce two
|
deba@2242
|
419 |
/// completly different sequences.
|
deba@2242
|
420 |
///
|
deba@2242
|
421 |
/// The generator gives back random numbers of serveral types. To
|
deba@2242
|
422 |
/// get a random number from a range of a floating point type you
|
deba@2245
|
423 |
/// can use one form of the \c operator() or the \c real() member
|
deba@2245
|
424 |
/// function. If you want to get random number from the {0, 1, ...,
|
deba@2245
|
425 |
/// n-1} integer range use the \c operator[] or the \c integer()
|
deba@2245
|
426 |
/// method. And to get random number from the whole range of an
|
deba@2245
|
427 |
/// integer type you can use the argumentless \c integer() or \c
|
deba@2245
|
428 |
/// uinteger() functions. After all you can get random bool with
|
deba@2245
|
429 |
/// equal chance of true and false or given probability of true
|
deba@2245
|
430 |
/// result with the \c boolean() member functions.
|
deba@2242
|
431 |
///
|
deba@2242
|
432 |
///\code
|
deba@2245
|
433 |
/// // The commented code is identical to the other
|
deba@2245
|
434 |
/// double a = rnd(); // [0.0, 1.0)
|
deba@2245
|
435 |
/// // double a = rnd.real(); // [0.0, 1.0)
|
deba@2245
|
436 |
/// double b = rnd(100.0); // [0.0, 100.0)
|
deba@2245
|
437 |
/// // double b = rnd.real(100.0); // [0.0, 100.0)
|
deba@2245
|
438 |
/// double c = rnd(1.0, 2.0); // [1.0, 2.0)
|
deba@2245
|
439 |
/// // double c = rnd.real(1.0, 2.0); // [1.0, 2.0)
|
deba@2245
|
440 |
/// int d = rnd[100000]; // 0..99999
|
deba@2245
|
441 |
/// // int d = rnd.integer(100000); // 0..99999
|
deba@2245
|
442 |
/// int e = rnd[6] + 1; // 1..6
|
deba@2245
|
443 |
/// // int e = rnd.integer(1, 1 + 6); // 1..6
|
deba@2242
|
444 |
/// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1
|
deba@2242
|
445 |
/// int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1
|
deba@2242
|
446 |
/// bool g = rnd.boolean(); // P(g = true) = 0.5
|
deba@2242
|
447 |
/// bool h = rnd.boolean(0.8); // P(h = true) = 0.8
|
deba@2242
|
448 |
///\endcode
|
deba@2242
|
449 |
///
|
deba@2245
|
450 |
/// The lemon provides a global instance of the random number
|
deba@2245
|
451 |
/// generator which name is \ref lemon::rnd "rnd". Usually it is a
|
deba@2245
|
452 |
/// good programming convenience to use this global generator to get
|
deba@2245
|
453 |
/// random numbers.
|
deba@2229
|
454 |
///
|
deba@2229
|
455 |
/// \author Balazs Dezso
|
deba@2229
|
456 |
class Random {
|
deba@2242
|
457 |
private:
|
deba@2229
|
458 |
|
deba@2245
|
459 |
// architecture word
|
deba@2242
|
460 |
typedef unsigned long Word;
|
deba@2242
|
461 |
|
deba@2242
|
462 |
_random_bits::RandomCore<Word> core;
|
deba@2229
|
463 |
|
deba@2229
|
464 |
public:
|
deba@2229
|
465 |
|
deba@2229
|
466 |
/// \brief Constructor
|
deba@2229
|
467 |
///
|
deba@2242
|
468 |
/// Constructor with constant seeding.
|
deba@2242
|
469 |
Random() { core.initState(); }
|
deba@2229
|
470 |
|
deba@2229
|
471 |
/// \brief Constructor
|
deba@2229
|
472 |
///
|
deba@2242
|
473 |
/// Constructor with seed. The current number type will be converted
|
deba@2242
|
474 |
/// to the architecture word type.
|
deba@2242
|
475 |
template <typename Number>
|
deba@2242
|
476 |
Random(Number seed) {
|
deba@2242
|
477 |
_random_bits::Initializer<Number, Word>::init(core, seed);
|
deba@2242
|
478 |
}
|
deba@2242
|
479 |
|
deba@2242
|
480 |
/// \brief Constructor
|
deba@2242
|
481 |
///
|
deba@2242
|
482 |
/// Constructor with array seeding. The given range should contain
|
deba@2242
|
483 |
/// any number type and the numbers will be converted to the
|
deba@2242
|
484 |
/// architecture word type.
|
deba@2242
|
485 |
template <typename Iterator>
|
deba@2242
|
486 |
Random(Iterator begin, Iterator end) {
|
deba@2242
|
487 |
typedef typename std::iterator_traits<Iterator>::value_type Number;
|
deba@2242
|
488 |
_random_bits::Initializer<Number, Word>::init(core, begin, end);
|
deba@2242
|
489 |
}
|
deba@2229
|
490 |
|
deba@2229
|
491 |
/// \brief Copy constructor
|
deba@2229
|
492 |
///
|
deba@2229
|
493 |
/// Copy constructor. The generated sequence will be identical to
|
deba@2245
|
494 |
/// the other sequence. It can be used to save the current state
|
deba@2245
|
495 |
/// of the generator and later use it to generate the same
|
deba@2245
|
496 |
/// sequence.
|
deba@2242
|
497 |
Random(const Random& other) {
|
deba@2242
|
498 |
core.copyState(other.core);
|
deba@2229
|
499 |
}
|
deba@2229
|
500 |
|
deba@2229
|
501 |
/// \brief Assign operator
|
deba@2229
|
502 |
///
|
deba@2229
|
503 |
/// Assign operator. The generated sequence will be identical to
|
deba@2245
|
504 |
/// the other sequence. It can be used to save the current state
|
deba@2245
|
505 |
/// of the generator and later use it to generate the same
|
deba@2245
|
506 |
/// sequence.
|
deba@2229
|
507 |
Random& operator=(const Random& other) {
|
deba@2229
|
508 |
if (&other != this) {
|
deba@2242
|
509 |
core.copyState(other.core);
|
deba@2229
|
510 |
}
|
deba@2229
|
511 |
return *this;
|
deba@2229
|
512 |
}
|
deba@2229
|
513 |
|
alpar@2257
|
514 |
/// \brief Returns a random real number from the range [0, 1)
|
deba@2229
|
515 |
///
|
deba@2245
|
516 |
/// It returns a random real number from the range [0, 1). The
|
deba@2245
|
517 |
/// default Number type is double.
|
deba@2242
|
518 |
template <typename Number>
|
deba@2245
|
519 |
Number real() {
|
deba@2242
|
520 |
return _random_bits::RealConversion<Number, Word>::convert(core);
|
deba@2229
|
521 |
}
|
deba@2229
|
522 |
|
deba@2245
|
523 |
double real() {
|
deba@2245
|
524 |
return real<double>();
|
deba@2245
|
525 |
}
|
deba@2245
|
526 |
|
alpar@2257
|
527 |
/// \brief Returns a random real number the range [0, b)
|
deba@2245
|
528 |
///
|
deba@2245
|
529 |
/// It returns a random real number from the range [0, b).
|
deba@2245
|
530 |
template <typename Number>
|
deba@2245
|
531 |
Number real(Number b) {
|
deba@2245
|
532 |
return real<Number>() * b;
|
deba@2245
|
533 |
}
|
deba@2245
|
534 |
|
alpar@2257
|
535 |
/// \brief Returns a random real number from the range [a, b)
|
deba@2245
|
536 |
///
|
deba@2245
|
537 |
/// It returns a random real number from the range [a, b).
|
deba@2245
|
538 |
template <typename Number>
|
deba@2245
|
539 |
Number real(Number a, Number b) {
|
deba@2245
|
540 |
return real<Number>() * (b - a) + a;
|
deba@2245
|
541 |
}
|
deba@2245
|
542 |
|
alpar@2257
|
543 |
/// \brief Returns a random real number from the range [0, 1)
|
deba@2245
|
544 |
///
|
deba@2245
|
545 |
/// It returns a random double from the range [0, 1).
|
deba@2242
|
546 |
double operator()() {
|
deba@2245
|
547 |
return real<double>();
|
deba@2242
|
548 |
}
|
deba@2242
|
549 |
|
alpar@2257
|
550 |
/// \brief Returns a random real number from the range [0, b)
|
deba@2229
|
551 |
///
|
deba@2242
|
552 |
/// It returns a random real number from the range [0, b).
|
deba@2242
|
553 |
template <typename Number>
|
deba@2242
|
554 |
Number operator()(Number b) {
|
deba@2245
|
555 |
return real<Number>() * b;
|
deba@2242
|
556 |
}
|
deba@2242
|
557 |
|
alpar@2257
|
558 |
/// \brief Returns a random real number from the range [a, b)
|
deba@2242
|
559 |
///
|
deba@2242
|
560 |
/// It returns a random real number from the range [a, b).
|
deba@2242
|
561 |
template <typename Number>
|
deba@2242
|
562 |
Number operator()(Number a, Number b) {
|
deba@2245
|
563 |
return real<Number>() * (b - a) + a;
|
deba@2242
|
564 |
}
|
deba@2242
|
565 |
|
deba@2242
|
566 |
/// \brief Returns a random integer from a range
|
deba@2242
|
567 |
///
|
deba@2245
|
568 |
/// It returns a random integer from the range {0, 1, ..., b - 1}.
|
deba@2242
|
569 |
template <typename Number>
|
deba@2245
|
570 |
Number integer(Number b) {
|
deba@2245
|
571 |
return _random_bits::Mapping<Number, Word>::map(core, b);
|
deba@2245
|
572 |
}
|
deba@2245
|
573 |
|
deba@2245
|
574 |
/// \brief Returns a random integer from a range
|
deba@2245
|
575 |
///
|
deba@2245
|
576 |
/// It returns a random integer from the range {a, a + 1, ..., b - 1}.
|
deba@2245
|
577 |
template <typename Number>
|
deba@2245
|
578 |
Number integer(Number a, Number b) {
|
deba@2245
|
579 |
return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
|
deba@2245
|
580 |
}
|
deba@2245
|
581 |
|
deba@2245
|
582 |
/// \brief Returns a random integer from a range
|
deba@2245
|
583 |
///
|
deba@2245
|
584 |
/// It returns a random integer from the range {0, 1, ..., b - 1}.
|
deba@2245
|
585 |
template <typename Number>
|
deba@2245
|
586 |
Number operator[](Number b) {
|
deba@2245
|
587 |
return _random_bits::Mapping<Number, Word>::map(core, b);
|
deba@2242
|
588 |
}
|
deba@2242
|
589 |
|
deba@2242
|
590 |
/// \brief Returns a random non-negative integer
|
deba@2242
|
591 |
///
|
deba@2242
|
592 |
/// It returns a random non-negative integer uniformly from the
|
deba@2242
|
593 |
/// whole range of the current \c Number type. The default result
|
deba@2242
|
594 |
/// type of this function is unsigned int.
|
deba@2242
|
595 |
template <typename Number>
|
deba@2242
|
596 |
Number uinteger() {
|
deba@2242
|
597 |
return _random_bits::IntConversion<Number, Word>::convert(core);
|
deba@2242
|
598 |
}
|
deba@2242
|
599 |
|
deba@2242
|
600 |
unsigned int uinteger() {
|
deba@2242
|
601 |
return uinteger<unsigned int>();
|
deba@2242
|
602 |
}
|
deba@2242
|
603 |
|
deba@2242
|
604 |
/// \brief Returns a random integer
|
deba@2242
|
605 |
///
|
deba@2242
|
606 |
/// It returns a random integer uniformly from the whole range of
|
deba@2242
|
607 |
/// the current \c Number type. The default result type of this
|
deba@2242
|
608 |
/// function is int.
|
deba@2242
|
609 |
template <typename Number>
|
deba@2242
|
610 |
Number integer() {
|
deba@2242
|
611 |
static const int nb = std::numeric_limits<Number>::digits +
|
deba@2242
|
612 |
(std::numeric_limits<Number>::is_signed ? 1 : 0);
|
deba@2242
|
613 |
return _random_bits::IntConversion<Number, Word, nb>::convert(core);
|
deba@2242
|
614 |
}
|
deba@2242
|
615 |
|
deba@2242
|
616 |
int integer() {
|
deba@2242
|
617 |
return integer<int>();
|
deba@2229
|
618 |
}
|
deba@2229
|
619 |
|
deba@2242
|
620 |
/// \brief Returns a random bool
|
deba@2229
|
621 |
///
|
deba@2242
|
622 |
/// It returns a random bool
|
deba@2242
|
623 |
bool boolean() {
|
deba@2242
|
624 |
return _random_bits::BoolConversion<Word>::convert(core);
|
deba@2229
|
625 |
}
|
deba@2229
|
626 |
|
alpar@2356
|
627 |
///\name Nonuniform distributions
|
alpar@2356
|
628 |
///
|
alpar@2356
|
629 |
|
alpar@2356
|
630 |
///@{
|
alpar@2356
|
631 |
|
deba@2229
|
632 |
/// \brief Returns a random bool
|
deba@2229
|
633 |
///
|
deba@2242
|
634 |
/// It returns a random bool with given probability of true result
|
deba@2242
|
635 |
bool boolean(double p) {
|
deba@2242
|
636 |
return operator()() < p;
|
deba@2229
|
637 |
}
|
alpar@2355
|
638 |
|
alpar@2355
|
639 |
/// Standard Gauss distribution
|
alpar@2355
|
640 |
|
alpar@2355
|
641 |
/// Standard Gauss distribution.
|
alpar@2356
|
642 |
/// \note The Cartesian form of the Box-Muller
|
alpar@2356
|
643 |
/// transformation is used to generate a random normal distribution.
|
alpar@2356
|
644 |
/// \todo Consider using the "ziggurat" method instead.
|
alpar@2355
|
645 |
double gauss()
|
alpar@2355
|
646 |
{
|
alpar@2355
|
647 |
double V1,V2,S;
|
alpar@2355
|
648 |
do {
|
alpar@2355
|
649 |
V1=2*real<double>()-1;
|
alpar@2355
|
650 |
V2=2*real<double>()-1;
|
alpar@2355
|
651 |
S=V1*V1+V2*V2;
|
alpar@2355
|
652 |
} while(S>=1);
|
alpar@2355
|
653 |
return std::sqrt(-2*std::log(S)/S)*V1;
|
alpar@2355
|
654 |
}
|
alpar@2356
|
655 |
/// Gauss distribution with given standard deviation and mean 0
|
alpar@2356
|
656 |
|
alpar@2356
|
657 |
/// \sa gauss()
|
alpar@2356
|
658 |
///
|
alpar@2356
|
659 |
double gauss(double std_dev)
|
alpar@2355
|
660 |
{
|
alpar@2356
|
661 |
return gauss()*std_dev;
|
alpar@2355
|
662 |
}
|
alpar@2356
|
663 |
/// Gauss distribution with given mean and standard deviation
|
alpar@2356
|
664 |
|
alpar@2356
|
665 |
/// \sa gauss()
|
alpar@2356
|
666 |
///
|
alpar@2356
|
667 |
double gauss(double mean,double std_dev)
|
alpar@2355
|
668 |
{
|
alpar@2356
|
669 |
return gauss()*std_dev+mean;
|
alpar@2355
|
670 |
}
|
alpar@2355
|
671 |
|
alpar@2356
|
672 |
/// Exponential distribution with given mean
|
alpar@2356
|
673 |
|
alpar@2356
|
674 |
/// This function generates an exponential distribution random number
|
alpar@2356
|
675 |
/// with mean <tt>1/lambda</tt>.
|
alpar@2356
|
676 |
///
|
alpar@2356
|
677 |
double exponential(double lambda=1.0)
|
alpar@2355
|
678 |
{
|
alpar@2355
|
679 |
return -log(real<double>())/lambda;
|
alpar@2355
|
680 |
}
|
alpar@2356
|
681 |
|
alpar@2356
|
682 |
///@}
|
deba@2229
|
683 |
|
deba@2229
|
684 |
};
|
deba@2229
|
685 |
|
deba@2229
|
686 |
|
deba@2229
|
687 |
extern Random rnd;
|
deba@2229
|
688 |
|
deba@2229
|
689 |
}
|
deba@2229
|
690 |
|
deba@2229
|
691 |
#endif
|