1 |
1 |
/* -*- C++ -*-
|
2 |
2 |
*
|
3 |
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
4 |
4 |
*
|
5 |
5 |
* Copyright (C) 2003-2007
|
6 |
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
7 |
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
8 |
8 |
*
|
9 |
9 |
* Permission to use, modify and distribute this software is granted
|
10 |
10 |
* provided that this copyright notice appears in all copies. For
|
11 |
11 |
* precise terms see the accompanying LICENSE file.
|
12 |
12 |
*
|
13 |
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
14 |
14 |
* express or implied, and with no claim as to its suitability for any
|
15 |
15 |
* purpose.
|
16 |
16 |
*
|
17 |
17 |
*/
|
18 |
18 |
|
19 |
19 |
/*
|
20 |
20 |
* This file contains the reimplemented version of the Mersenne Twister
|
21 |
21 |
* Generator of Matsumoto and Nishimura.
|
22 |
22 |
*
|
23 |
23 |
* See the appropriate copyright notice below.
|
24 |
24 |
*
|
25 |
25 |
* Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
26 |
26 |
* All rights reserved.
|
27 |
27 |
*
|
28 |
28 |
* Redistribution and use in source and binary forms, with or without
|
29 |
29 |
* modification, are permitted provided that the following conditions
|
30 |
30 |
* are met:
|
31 |
31 |
*
|
32 |
32 |
* 1. Redistributions of source code must retain the above copyright
|
33 |
33 |
* notice, this list of conditions and the following disclaimer.
|
34 |
34 |
*
|
35 |
35 |
* 2. Redistributions in binary form must reproduce the above copyright
|
36 |
36 |
* notice, this list of conditions and the following disclaimer in the
|
37 |
37 |
* documentation and/or other materials provided with the distribution.
|
38 |
38 |
*
|
39 |
39 |
* 3. The names of its contributors may not be used to endorse or promote
|
40 |
40 |
* products derived from this software without specific prior written
|
41 |
41 |
* permission.
|
42 |
42 |
*
|
43 |
43 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
44 |
44 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
45 |
45 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
46 |
46 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
47 |
47 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
48 |
48 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
49 |
49 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
50 |
50 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
51 |
51 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
52 |
52 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
53 |
53 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
54 |
54 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
55 |
55 |
*
|
56 |
56 |
*
|
57 |
57 |
* Any feedback is very welcome.
|
58 |
58 |
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
59 |
59 |
* email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
|
60 |
60 |
*/
|
61 |
61 |
|
62 |
62 |
#ifndef LEMON_RANDOM_H
|
63 |
63 |
#define LEMON_RANDOM_H
|
64 |
64 |
|
65 |
65 |
#include <algorithm>
|
66 |
66 |
#include <iterator>
|
67 |
67 |
#include <vector>
|
68 |
68 |
|
69 |
69 |
#include <ctime>
|
70 |
70 |
#include <cmath>
|
71 |
71 |
|
72 |
72 |
#include <lemon/dim2.h>
|
73 |
73 |
///\ingroup misc
|
74 |
74 |
///\file
|
75 |
75 |
///\brief Mersenne Twister random number generator
|
76 |
|
///
|
77 |
|
///\author Balazs Dezso
|
78 |
76 |
|
79 |
77 |
namespace lemon {
|
80 |
78 |
|
81 |
79 |
namespace _random_bits {
|
82 |
80 |
|
83 |
81 |
template <typename _Word, int _bits = std::numeric_limits<_Word>::digits>
|
84 |
82 |
struct RandomTraits {};
|
85 |
83 |
|
86 |
84 |
template <typename _Word>
|
87 |
85 |
struct RandomTraits<_Word, 32> {
|
88 |
86 |
|
89 |
87 |
typedef _Word Word;
|
90 |
88 |
static const int bits = 32;
|
91 |
89 |
|
92 |
90 |
static const int length = 624;
|
93 |
91 |
static const int shift = 397;
|
94 |
92 |
|
95 |
93 |
static const Word mul = 0x6c078965u;
|
96 |
94 |
static const Word arrayInit = 0x012BD6AAu;
|
97 |
95 |
static const Word arrayMul1 = 0x0019660Du;
|
98 |
96 |
static const Word arrayMul2 = 0x5D588B65u;
|
99 |
97 |
|
100 |
98 |
static const Word mask = 0x9908B0DFu;
|
101 |
99 |
static const Word loMask = (1u << 31) - 1;
|
102 |
100 |
static const Word hiMask = ~loMask;
|
103 |
101 |
|
104 |
102 |
|
105 |
103 |
static Word tempering(Word rnd) {
|
106 |
104 |
rnd ^= (rnd >> 11);
|
107 |
105 |
rnd ^= (rnd << 7) & 0x9D2C5680u;
|
108 |
106 |
rnd ^= (rnd << 15) & 0xEFC60000u;
|
109 |
107 |
rnd ^= (rnd >> 18);
|
110 |
108 |
return rnd;
|
111 |
109 |
}
|
112 |
110 |
|
113 |
111 |
};
|
114 |
112 |
|
115 |
113 |
template <typename _Word>
|
116 |
114 |
struct RandomTraits<_Word, 64> {
|
117 |
115 |
|
118 |
116 |
typedef _Word Word;
|
119 |
117 |
static const int bits = 64;
|
120 |
118 |
|
121 |
119 |
static const int length = 312;
|
122 |
120 |
static const int shift = 156;
|
123 |
121 |
|
124 |
122 |
static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du);
|
125 |
123 |
static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu);
|
126 |
124 |
static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u);
|
127 |
125 |
static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu);
|
128 |
126 |
|
129 |
127 |
static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u);
|
130 |
128 |
static const Word loMask = (Word(1u) << 31) - 1;
|
131 |
129 |
static const Word hiMask = ~loMask;
|
132 |
130 |
|
133 |
131 |
static Word tempering(Word rnd) {
|
134 |
132 |
rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u));
|
135 |
133 |
rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u));
|
136 |
134 |
rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u));
|
137 |
135 |
rnd ^= (rnd >> 43);
|
138 |
136 |
return rnd;
|
139 |
137 |
}
|
140 |
138 |
|
141 |
139 |
};
|
142 |
140 |
|
143 |
141 |
template <typename _Word>
|
144 |
142 |
class RandomCore {
|
145 |
143 |
public:
|
146 |
144 |
|
147 |
145 |
typedef _Word Word;
|
148 |
146 |
|
149 |
147 |
private:
|
150 |
148 |
|
151 |
149 |
static const int bits = RandomTraits<Word>::bits;
|
152 |
150 |
|
153 |
151 |
static const int length = RandomTraits<Word>::length;
|
154 |
152 |
static const int shift = RandomTraits<Word>::shift;
|
155 |
153 |
|
156 |
154 |
public:
|
157 |
155 |
|
158 |
156 |
void initState() {
|
159 |
157 |
static const Word seedArray[4] = {
|
160 |
158 |
0x12345u, 0x23456u, 0x34567u, 0x45678u
|
161 |
159 |
};
|
162 |
160 |
|
163 |
161 |
initState(seedArray, seedArray + 4);
|
164 |
162 |
}
|
165 |
163 |
|
166 |
164 |
void initState(Word seed) {
|
167 |
165 |
|
168 |
166 |
static const Word mul = RandomTraits<Word>::mul;
|
169 |
167 |
|
170 |
168 |
current = state;
|
171 |
169 |
|
172 |
170 |
Word *curr = state + length - 1;
|
173 |
171 |
curr[0] = seed; --curr;
|
174 |
172 |
for (int i = 1; i < length; ++i) {
|
175 |
173 |
curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i);
|
176 |
174 |
--curr;
|
177 |
175 |
}
|
178 |
176 |
}
|
179 |
177 |
|
180 |
178 |
template <typename Iterator>
|
181 |
179 |
void initState(Iterator begin, Iterator end) {
|
182 |
180 |
|
183 |
181 |
static const Word init = RandomTraits<Word>::arrayInit;
|
184 |
182 |
static const Word mul1 = RandomTraits<Word>::arrayMul1;
|
185 |
183 |
static const Word mul2 = RandomTraits<Word>::arrayMul2;
|
186 |
184 |
|
187 |
185 |
|
188 |
186 |
Word *curr = state + length - 1; --curr;
|
189 |
187 |
Iterator it = begin; int cnt = 0;
|
190 |
188 |
int num;
|
191 |
189 |
|
192 |
190 |
initState(init);
|
193 |
191 |
|
194 |
192 |
num = length > end - begin ? length : end - begin;
|
195 |
193 |
while (num--) {
|
196 |
194 |
curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1))
|
197 |
195 |
+ *it + cnt;
|
198 |
196 |
++it; ++cnt;
|
199 |
197 |
if (it == end) {
|
200 |
198 |
it = begin; cnt = 0;
|
201 |
199 |
}
|
202 |
200 |
if (curr == state) {
|
203 |
201 |
curr = state + length - 1; curr[0] = state[0];
|
204 |
202 |
}
|
205 |
203 |
--curr;
|
206 |
204 |
}
|
207 |
205 |
|
208 |
206 |
num = length - 1; cnt = length - (curr - state) - 1;
|
209 |
207 |
while (num--) {
|
210 |
208 |
curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2))
|
211 |
209 |
- cnt;
|
212 |
210 |
--curr; ++cnt;
|
213 |
211 |
if (curr == state) {
|
214 |
212 |
curr = state + length - 1; curr[0] = state[0]; --curr;
|
215 |
213 |
cnt = 1;
|
216 |
214 |
}
|
217 |
215 |
}
|
218 |
216 |
|
219 |
217 |
state[length - 1] = Word(1) << (bits - 1);
|
220 |
218 |
}
|
221 |
219 |
|
222 |
220 |
void copyState(const RandomCore& other) {
|
223 |
221 |
std::copy(other.state, other.state + length, state);
|
224 |
222 |
current = state + (other.current - other.state);
|
225 |
223 |
}
|
226 |
224 |
|
227 |
225 |
Word operator()() {
|
228 |
226 |
if (current == state) fillState();
|
229 |
227 |
--current;
|
230 |
228 |
Word rnd = *current;
|
231 |
229 |
return RandomTraits<Word>::tempering(rnd);
|
232 |
230 |
}
|
233 |
231 |
|
234 |
232 |
private:
|
235 |
233 |
|
236 |
234 |
|
237 |
235 |
void fillState() {
|
238 |
236 |
static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
|
239 |
237 |
static const Word loMask = RandomTraits<Word>::loMask;
|
240 |
238 |
static const Word hiMask = RandomTraits<Word>::hiMask;
|
241 |
239 |
|
242 |
240 |
current = state + length;
|
243 |
241 |
|
244 |
242 |
register Word *curr = state + length - 1;
|
245 |
243 |
register long num;
|
246 |
244 |
|
247 |
245 |
num = length - shift;
|
248 |
246 |
while (num--) {
|
249 |
247 |
curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
|
250 |
248 |
curr[- shift] ^ mask[curr[-1] & 1ul];
|
251 |
249 |
--curr;
|
252 |
250 |
}
|
253 |
251 |
num = shift - 1;
|
254 |
252 |
while (num--) {
|
255 |
253 |
curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
|
256 |
254 |
curr[length - shift] ^ mask[curr[-1] & 1ul];
|
257 |
255 |
--curr;
|
258 |
256 |
}
|
259 |
257 |
curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
|
260 |
258 |
curr[length - shift] ^ mask[curr[length - 1] & 1ul];
|
261 |
259 |
|
262 |
260 |
}
|
263 |
261 |
|
264 |
262 |
|
265 |
263 |
Word *current;
|
266 |
264 |
Word state[length];
|
267 |
265 |
|
268 |
266 |
};
|
269 |
267 |
|
270 |
268 |
|
271 |
269 |
template <typename Result,
|
272 |
270 |
int shift = (std::numeric_limits<Result>::digits + 1) / 2>
|
273 |
271 |
struct Masker {
|
274 |
272 |
static Result mask(const Result& result) {
|
275 |
273 |
return Masker<Result, (shift + 1) / 2>::
|
276 |
274 |
mask(static_cast<Result>(result | (result >> shift)));
|
277 |
275 |
}
|
278 |
276 |
};
|
279 |
277 |
|
280 |
278 |
template <typename Result>
|
281 |
279 |
struct Masker<Result, 1> {
|
282 |
280 |
static Result mask(const Result& result) {
|
283 |
281 |
return static_cast<Result>(result | (result >> 1));
|
284 |
282 |
}
|
285 |
283 |
};
|
286 |
284 |
|
287 |
285 |
template <typename Result, typename Word,
|
288 |
286 |
int rest = std::numeric_limits<Result>::digits, int shift = 0,
|
289 |
287 |
bool last = rest <= std::numeric_limits<Word>::digits>
|
290 |
288 |
struct IntConversion {
|
291 |
289 |
static const int bits = std::numeric_limits<Word>::digits;
|
292 |
290 |
|
293 |
291 |
static Result convert(RandomCore<Word>& rnd) {
|
294 |
292 |
return static_cast<Result>(rnd() >> (bits - rest)) << shift;
|
295 |
293 |
}
|
296 |
294 |
|
297 |
295 |
};
|
298 |
296 |
|
299 |
297 |
template <typename Result, typename Word, int rest, int shift>
|
300 |
298 |
struct IntConversion<Result, Word, rest, shift, false> {
|
301 |
299 |
static const int bits = std::numeric_limits<Word>::digits;
|
302 |
300 |
|
303 |
301 |
static Result convert(RandomCore<Word>& rnd) {
|
304 |
302 |
return (static_cast<Result>(rnd()) << shift) |
|
305 |
303 |
IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd);
|
306 |
304 |
}
|
307 |
305 |
};
|
308 |
306 |
|
309 |
307 |
|
310 |
308 |
template <typename Result, typename Word,
|
311 |
309 |
bool one_word = (std::numeric_limits<Word>::digits <
|
312 |
310 |
std::numeric_limits<Result>::digits) >
|
313 |
311 |
struct Mapping {
|
314 |
312 |
static Result map(RandomCore<Word>& rnd, const Result& bound) {
|
315 |
313 |
Word max = Word(bound - 1);
|
316 |
314 |
Result mask = Masker<Result>::mask(bound - 1);
|
317 |
315 |
Result num;
|
318 |
316 |
do {
|
319 |
317 |
num = IntConversion<Result, Word>::convert(rnd) & mask;
|
320 |
318 |
} while (num > max);
|
321 |
319 |
return num;
|
322 |
320 |
}
|
323 |
321 |
};
|
324 |
322 |
|
325 |
323 |
template <typename Result, typename Word>
|
326 |
324 |
struct Mapping<Result, Word, false> {
|
327 |
325 |
static Result map(RandomCore<Word>& rnd, const Result& bound) {
|
328 |
326 |
Word max = Word(bound - 1);
|
329 |
327 |
Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2>
|
330 |
328 |
::mask(max);
|
331 |
329 |
Word num;
|
332 |
330 |
do {
|
333 |
331 |
num = rnd() & mask;
|
334 |
332 |
} while (num > max);
|
335 |
333 |
return num;
|
336 |
334 |
}
|
337 |
335 |
};
|
338 |
336 |
|
339 |
337 |
template <typename Result, int exp, bool pos = (exp >= 0)>
|
340 |
338 |
struct ShiftMultiplier {
|
341 |
339 |
static const Result multiplier() {
|
342 |
340 |
Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
|
343 |
341 |
res *= res;
|
344 |
342 |
if ((exp & 1) == 1) res *= static_cast<Result>(2.0);
|
345 |
343 |
return res;
|
346 |
344 |
}
|
347 |
345 |
};
|
348 |
346 |
|
349 |
347 |
template <typename Result, int exp>
|
350 |
348 |
struct ShiftMultiplier<Result, exp, false> {
|
351 |
349 |
static const Result multiplier() {
|
352 |
350 |
Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
|
353 |
351 |
res *= res;
|
354 |
352 |
if ((exp & 1) == 1) res *= static_cast<Result>(0.5);
|
355 |
353 |
return res;
|
356 |
354 |
}
|
357 |
355 |
};
|
358 |
356 |
|
359 |
357 |
template <typename Result>
|
360 |
358 |
struct ShiftMultiplier<Result, 0, true> {
|
361 |
359 |
static const Result multiplier() {
|
362 |
360 |
return static_cast<Result>(1.0);
|
363 |
361 |
}
|
364 |
362 |
};
|
365 |
363 |
|
366 |
364 |
template <typename Result>
|
367 |
365 |
struct ShiftMultiplier<Result, -20, true> {
|
368 |
366 |
static const Result multiplier() {
|
369 |
367 |
return static_cast<Result>(1.0/1048576.0);
|
370 |
368 |
}
|
371 |
369 |
};
|
372 |
370 |
|
373 |
371 |
template <typename Result>
|
374 |
372 |
struct ShiftMultiplier<Result, -32, true> {
|
375 |
373 |
static const Result multiplier() {
|
376 |
374 |
return static_cast<Result>(1.0/424967296.0);
|
377 |
375 |
}
|
378 |
376 |
};
|
379 |
377 |
|
380 |
378 |
template <typename Result>
|
381 |
379 |
struct ShiftMultiplier<Result, -53, true> {
|
382 |
380 |
static const Result multiplier() {
|
383 |
381 |
return static_cast<Result>(1.0/9007199254740992.0);
|
384 |
382 |
}
|
385 |
383 |
};
|
386 |
384 |
|
387 |
385 |
template <typename Result>
|
388 |
386 |
struct ShiftMultiplier<Result, -64, true> {
|
389 |
387 |
static const Result multiplier() {
|
390 |
388 |
return static_cast<Result>(1.0/18446744073709551616.0);
|
391 |
389 |
}
|
392 |
390 |
};
|
393 |
391 |
|
394 |
392 |
template <typename Result, int exp>
|
395 |
393 |
struct Shifting {
|
396 |
394 |
static Result shift(const Result& result) {
|
397 |
395 |
return result * ShiftMultiplier<Result, exp>::multiplier();
|
398 |
396 |
}
|
399 |
397 |
};
|
400 |
398 |
|
401 |
399 |
template <typename Result, typename Word,
|
402 |
400 |
int rest = std::numeric_limits<Result>::digits, int shift = 0,
|
403 |
401 |
bool last = rest <= std::numeric_limits<Word>::digits>
|
404 |
402 |
struct RealConversion{
|
405 |
403 |
static const int bits = std::numeric_limits<Word>::digits;
|
406 |
404 |
|
407 |
405 |
static Result convert(RandomCore<Word>& rnd) {
|
408 |
406 |
return Shifting<Result, - shift - rest>::
|
409 |
407 |
shift(static_cast<Result>(rnd() >> (bits - rest)));
|
410 |
408 |
}
|
411 |
409 |
};
|
412 |
410 |
|
413 |
411 |
template <typename Result, typename Word, int rest, int shift>
|
414 |
412 |
struct RealConversion<Result, Word, rest, shift, false> {
|
415 |
413 |
static const int bits = std::numeric_limits<Word>::digits;
|
416 |
414 |
|
417 |
415 |
static Result convert(RandomCore<Word>& rnd) {
|
418 |
416 |
return Shifting<Result, - shift - bits>::
|
419 |
417 |
shift(static_cast<Result>(rnd())) +
|
420 |
418 |
RealConversion<Result, Word, rest-bits, shift + bits>::
|
421 |
419 |
convert(rnd);
|
422 |
420 |
}
|
423 |
421 |
};
|
424 |
422 |
|
425 |
423 |
template <typename Result, typename Word>
|
426 |
424 |
struct Initializer {
|
427 |
425 |
|
428 |
426 |
template <typename Iterator>
|
429 |
427 |
static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) {
|
430 |
428 |
std::vector<Word> ws;
|
431 |
429 |
for (Iterator it = begin; it != end; ++it) {
|
432 |
430 |
ws.push_back(Word(*it));
|
433 |
431 |
}
|
434 |
432 |
rnd.initState(ws.begin(), ws.end());
|
435 |
433 |
}
|
436 |
434 |
|
437 |
435 |
static void init(RandomCore<Word>& rnd, Result seed) {
|
438 |
436 |
rnd.initState(seed);
|
439 |
437 |
}
|
440 |
438 |
};
|
441 |
439 |
|
442 |
440 |
template <typename Word>
|
443 |
441 |
struct BoolConversion {
|
444 |
442 |
static bool convert(RandomCore<Word>& rnd) {
|
445 |
443 |
return (rnd() & 1) == 1;
|
446 |
444 |
}
|
447 |
445 |
};
|
448 |
446 |
|
449 |
447 |
template <typename Word>
|
450 |
448 |
struct BoolProducer {
|
451 |
449 |
Word buffer;
|
452 |
450 |
int num;
|
453 |
451 |
|
454 |
452 |
BoolProducer() : num(0) {}
|
455 |
453 |
|
456 |
454 |
bool convert(RandomCore<Word>& rnd) {
|
457 |
455 |
if (num == 0) {
|
458 |
456 |
buffer = rnd();
|
459 |
457 |
num = RandomTraits<Word>::bits;
|
460 |
458 |
}
|
461 |
459 |
bool r = (buffer & 1);
|
462 |
460 |
buffer >>= 1;
|
463 |
461 |
--num;
|
464 |
462 |
return r;
|
465 |
463 |
}
|
466 |
464 |
};
|
467 |
465 |
|
468 |
466 |
}
|
469 |
467 |
|
470 |
468 |
/// \ingroup misc
|
471 |
469 |
///
|
472 |
470 |
/// \brief Mersenne Twister random number generator
|
473 |
471 |
///
|
474 |
472 |
/// The Mersenne Twister is a twisted generalized feedback
|
475 |
473 |
/// shift-register generator of Matsumoto and Nishimura. The period
|
476 |
474 |
/// of this generator is \f$ 2^{19937} - 1 \f$ and it is
|
477 |
475 |
/// equi-distributed in 623 dimensions for 32-bit numbers. The time
|
478 |
476 |
/// performance of this generator is comparable to the commonly used
|
479 |
477 |
/// generators.
|
480 |
478 |
///
|
481 |
479 |
/// This implementation is specialized for both 32-bit and 64-bit
|
482 |
480 |
/// architectures. The generators differ sligthly in the
|
483 |
481 |
/// initialization and generation phase so they produce two
|
484 |
482 |
/// completly different sequences.
|
485 |
483 |
///
|
486 |
484 |
/// The generator gives back random numbers of serveral types. To
|
487 |
485 |
/// get a random number from a range of a floating point type you
|
488 |
486 |
/// can use one form of the \c operator() or the \c real() member
|
489 |
487 |
/// function. If you want to get random number from the {0, 1, ...,
|
490 |
488 |
/// n-1} integer range use the \c operator[] or the \c integer()
|
491 |
489 |
/// method. And to get random number from the whole range of an
|
492 |
490 |
/// integer type you can use the argumentless \c integer() or \c
|
493 |
491 |
/// uinteger() functions. After all you can get random bool with
|
494 |
492 |
/// equal chance of true and false or given probability of true
|
495 |
493 |
/// result with the \c boolean() member functions.
|
496 |
494 |
///
|
497 |
495 |
///\code
|
498 |
496 |
/// // The commented code is identical to the other
|
499 |
497 |
/// double a = rnd(); // [0.0, 1.0)
|
500 |
498 |
/// // double a = rnd.real(); // [0.0, 1.0)
|
501 |
499 |
/// double b = rnd(100.0); // [0.0, 100.0)
|
502 |
500 |
/// // double b = rnd.real(100.0); // [0.0, 100.0)
|
503 |
501 |
/// double c = rnd(1.0, 2.0); // [1.0, 2.0)
|
504 |
502 |
/// // double c = rnd.real(1.0, 2.0); // [1.0, 2.0)
|
505 |
503 |
/// int d = rnd[100000]; // 0..99999
|
506 |
504 |
/// // int d = rnd.integer(100000); // 0..99999
|
507 |
505 |
/// int e = rnd[6] + 1; // 1..6
|
508 |
506 |
/// // int e = rnd.integer(1, 1 + 6); // 1..6
|
509 |
507 |
/// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1
|
510 |
508 |
/// int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1
|
511 |
509 |
/// bool g = rnd.boolean(); // P(g = true) = 0.5
|
512 |
510 |
/// bool h = rnd.boolean(0.8); // P(h = true) = 0.8
|
513 |
511 |
///\endcode
|
514 |
512 |
///
|
515 |
513 |
/// The lemon provides a global instance of the random number
|
516 |
514 |
/// generator which name is \ref lemon::rnd "rnd". Usually it is a
|
517 |
515 |
/// good programming convenience to use this global generator to get
|
518 |
516 |
/// random numbers.
|
519 |
|
///
|
520 |
|
/// \author Balazs Dezso
|
521 |
517 |
class Random {
|
522 |
518 |
private:
|
523 |
519 |
|
524 |
|
// architecture word
|
|
520 |
// Architecture word
|
525 |
521 |
typedef unsigned long Word;
|
526 |
522 |
|
527 |
523 |
_random_bits::RandomCore<Word> core;
|
528 |
524 |
_random_bits::BoolProducer<Word> bool_producer;
|
529 |
525 |
|
530 |
526 |
|
531 |
527 |
public:
|
532 |
528 |
|
533 |
529 |
/// \brief Constructor
|
534 |
530 |
///
|
535 |
531 |
/// Constructor with constant seeding.
|
536 |
532 |
Random() { core.initState(); }
|
537 |
533 |
|
538 |
534 |
/// \brief Constructor
|
539 |
535 |
///
|
540 |
536 |
/// Constructor with seed. The current number type will be converted
|
541 |
537 |
/// to the architecture word type.
|
542 |
538 |
template <typename Number>
|
543 |
539 |
Random(Number seed) {
|
544 |
540 |
_random_bits::Initializer<Number, Word>::init(core, seed);
|
545 |
541 |
}
|
546 |
542 |
|
547 |
543 |
/// \brief Constructor
|
548 |
544 |
///
|
549 |
545 |
/// Constructor with array seeding. The given range should contain
|
550 |
546 |
/// any number type and the numbers will be converted to the
|
551 |
547 |
/// architecture word type.
|
552 |
548 |
template <typename Iterator>
|
553 |
549 |
Random(Iterator begin, Iterator end) {
|
554 |
550 |
typedef typename std::iterator_traits<Iterator>::value_type Number;
|
555 |
551 |
_random_bits::Initializer<Number, Word>::init(core, begin, end);
|
556 |
552 |
}
|
557 |
553 |
|
558 |
554 |
/// \brief Copy constructor
|
559 |
555 |
///
|
560 |
556 |
/// Copy constructor. The generated sequence will be identical to
|
561 |
557 |
/// the other sequence. It can be used to save the current state
|
562 |
558 |
/// of the generator and later use it to generate the same
|
563 |
559 |
/// sequence.
|
564 |
560 |
Random(const Random& other) {
|
565 |
561 |
core.copyState(other.core);
|
566 |
562 |
}
|
567 |
563 |
|
568 |
564 |
/// \brief Assign operator
|
569 |
565 |
///
|
570 |
566 |
/// Assign operator. The generated sequence will be identical to
|
571 |
567 |
/// the other sequence. It can be used to save the current state
|
572 |
568 |
/// of the generator and later use it to generate the same
|
573 |
569 |
/// sequence.
|
574 |
570 |
Random& operator=(const Random& other) {
|
575 |
571 |
if (&other != this) {
|
576 |
572 |
core.copyState(other.core);
|
577 |
573 |
}
|
578 |
574 |
return *this;
|
579 |
575 |
}
|
580 |
576 |
|
581 |
577 |
/// \brief Returns a random real number from the range [0, 1)
|
582 |
578 |
///
|
583 |
579 |
/// It returns a random real number from the range [0, 1). The
|
584 |
580 |
/// default Number type is double.
|
585 |
581 |
template <typename Number>
|
586 |
582 |
Number real() {
|
587 |
583 |
return _random_bits::RealConversion<Number, Word>::convert(core);
|
588 |
584 |
}
|
589 |
585 |
|
590 |
586 |
double real() {
|
591 |
587 |
return real<double>();
|
592 |
588 |
}
|
593 |
589 |
|
594 |
590 |
/// \brief Returns a random real number the range [0, b)
|
595 |
591 |
///
|
596 |
592 |
/// It returns a random real number from the range [0, b).
|
597 |
593 |
template <typename Number>
|
598 |
594 |
Number real(Number b) {
|
599 |
595 |
return real<Number>() * b;
|
600 |
596 |
}
|
601 |
597 |
|
602 |
598 |
/// \brief Returns a random real number from the range [a, b)
|
603 |
599 |
///
|
604 |
600 |
/// It returns a random real number from the range [a, b).
|
605 |
601 |
template <typename Number>
|
606 |
602 |
Number real(Number a, Number b) {
|
607 |
603 |
return real<Number>() * (b - a) + a;
|
608 |
604 |
}
|
609 |
605 |
|
610 |
606 |
/// \brief Returns a random real number from the range [0, 1)
|
611 |
607 |
///
|
612 |
608 |
/// It returns a random double from the range [0, 1).
|
613 |
609 |
double operator()() {
|
614 |
610 |
return real<double>();
|
615 |
611 |
}
|
616 |
612 |
|
617 |
613 |
/// \brief Returns a random real number from the range [0, b)
|
618 |
614 |
///
|
619 |
615 |
/// It returns a random real number from the range [0, b).
|
620 |
616 |
template <typename Number>
|
621 |
617 |
Number operator()(Number b) {
|
622 |
618 |
return real<Number>() * b;
|
623 |
619 |
}
|
624 |
620 |
|
625 |
621 |
/// \brief Returns a random real number from the range [a, b)
|
626 |
622 |
///
|
627 |
623 |
/// It returns a random real number from the range [a, b).
|
628 |
624 |
template <typename Number>
|
629 |
625 |
Number operator()(Number a, Number b) {
|
630 |
626 |
return real<Number>() * (b - a) + a;
|
631 |
627 |
}
|
632 |
628 |
|
633 |
629 |
/// \brief Returns a random integer from a range
|
634 |
630 |
///
|
635 |
631 |
/// It returns a random integer from the range {0, 1, ..., b - 1}.
|
636 |
632 |
template <typename Number>
|
637 |
633 |
Number integer(Number b) {
|
638 |
634 |
return _random_bits::Mapping<Number, Word>::map(core, b);
|
639 |
635 |
}
|
640 |
636 |
|
641 |
637 |
/// \brief Returns a random integer from a range
|
642 |
638 |
///
|
643 |
639 |
/// It returns a random integer from the range {a, a + 1, ..., b - 1}.
|
644 |
640 |
template <typename Number>
|
645 |
641 |
Number integer(Number a, Number b) {
|
646 |
642 |
return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
|
647 |
643 |
}
|
648 |
644 |
|
649 |
645 |
/// \brief Returns a random integer from a range
|
650 |
646 |
///
|
651 |
647 |
/// It returns a random integer from the range {0, 1, ..., b - 1}.
|
652 |
648 |
template <typename Number>
|
653 |
649 |
Number operator[](Number b) {
|
654 |
650 |
return _random_bits::Mapping<Number, Word>::map(core, b);
|
655 |
651 |
}
|
656 |
652 |
|
657 |
653 |
/// \brief Returns a random non-negative integer
|
658 |
654 |
///
|
659 |
655 |
/// It returns a random non-negative integer uniformly from the
|
660 |
656 |
/// whole range of the current \c Number type. The default result
|
661 |
657 |
/// type of this function is unsigned int.
|
662 |
658 |
template <typename Number>
|
663 |
659 |
Number uinteger() {
|
664 |
660 |
return _random_bits::IntConversion<Number, Word>::convert(core);
|
665 |
661 |
}
|
666 |
662 |
|
667 |
663 |
unsigned int uinteger() {
|
668 |
664 |
return uinteger<unsigned int>();
|
669 |
665 |
}
|
670 |
666 |
|
671 |
667 |
/// \brief Returns a random integer
|
672 |
668 |
///
|
673 |
669 |
/// It returns a random integer uniformly from the whole range of
|
674 |
670 |
/// the current \c Number type. The default result type of this
|
675 |
671 |
/// function is int.
|
676 |
672 |
template <typename Number>
|
677 |
673 |
Number integer() {
|
678 |
674 |
static const int nb = std::numeric_limits<Number>::digits +
|
679 |
675 |
(std::numeric_limits<Number>::is_signed ? 1 : 0);
|
680 |
676 |
return _random_bits::IntConversion<Number, Word, nb>::convert(core);
|
681 |
677 |
}
|
682 |
678 |
|
683 |
679 |
int integer() {
|
684 |
680 |
return integer<int>();
|
685 |
681 |
}
|
686 |
682 |
|
687 |
683 |
/// \brief Returns a random bool
|
688 |
684 |
///
|
689 |
685 |
/// It returns a random bool. The generator holds a buffer for
|
690 |
686 |
/// random bits. Every time when it become empty the generator makes
|
691 |
687 |
/// a new random word and fill the buffer up.
|
692 |
688 |
bool boolean() {
|
693 |
689 |
return bool_producer.convert(core);
|
694 |
690 |
}
|
695 |
691 |
|
696 |
692 |
///\name Nonuniform distributions
|
697 |
693 |
///
|
698 |
694 |
|
699 |
695 |
///@{
|
700 |
696 |
|
701 |
697 |
/// \brief Returns a random bool
|
702 |
698 |
///
|
703 |
699 |
/// It returns a random bool with given probability of true result
|
704 |
700 |
bool boolean(double p) {
|
705 |
701 |
return operator()() < p;
|
706 |
702 |
}
|
707 |
703 |
|
708 |
704 |
/// Standard Gauss distribution
|
709 |
705 |
|
710 |
706 |
/// Standard Gauss distribution.
|
711 |
707 |
/// \note The Cartesian form of the Box-Muller
|
712 |
708 |
/// transformation is used to generate a random normal distribution.
|
713 |
709 |
/// \todo Consider using the "ziggurat" method instead.
|
714 |
710 |
double gauss()
|
715 |
711 |
{
|
716 |
712 |
double V1,V2,S;
|
717 |
713 |
do {
|
718 |
714 |
V1=2*real<double>()-1;
|
719 |
715 |
V2=2*real<double>()-1;
|
720 |
716 |
S=V1*V1+V2*V2;
|
721 |
717 |
} while(S>=1);
|
722 |
718 |
return std::sqrt(-2*std::log(S)/S)*V1;
|
723 |
719 |
}
|
724 |
720 |
/// Gauss distribution with given mean and standard deviation
|
725 |
721 |
|
|
722 |
/// Gauss distribution with given mean and standard deviation
|
726 |
723 |
/// \sa gauss()
|
727 |
|
///
|
728 |
724 |
double gauss(double mean,double std_dev)
|
729 |
725 |
{
|
730 |
726 |
return gauss()*std_dev+mean;
|
731 |
727 |
}
|
732 |
728 |
|
733 |
729 |
/// Exponential distribution with given mean
|
734 |
730 |
|
735 |
731 |
/// This function generates an exponential distribution random number
|
736 |
732 |
/// with mean <tt>1/lambda</tt>.
|
737 |
733 |
///
|
738 |
734 |
double exponential(double lambda=1.0)
|
739 |
735 |
{
|
740 |
736 |
return -std::log(1.0-real<double>())/lambda;
|
741 |
737 |
}
|
742 |
738 |
|
743 |
739 |
/// Gamma distribution with given integer shape
|
744 |
740 |
|
745 |
741 |
/// This function generates a gamma distribution random number.
|
746 |
742 |
///
|
747 |
743 |
///\param k shape parameter (<tt>k>0</tt> integer)
|
748 |
744 |
double gamma(int k)
|
749 |
745 |
{
|
750 |
746 |
double s = 0;
|
751 |
747 |
for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
|
752 |
748 |
return s;
|
753 |
749 |
}
|
754 |
750 |
|
755 |
751 |
/// Gamma distribution with given shape and scale parameter
|
756 |
752 |
|
757 |
753 |
/// This function generates a gamma distribution random number.
|
758 |
754 |
///
|
759 |
755 |
///\param k shape parameter (<tt>k>0</tt>)
|
760 |
756 |
///\param theta scale parameter
|
761 |
757 |
///
|
762 |
758 |
double gamma(double k,double theta=1.0)
|
763 |
759 |
{
|
764 |
760 |
double xi,nu;
|
765 |
761 |
const double delta = k-std::floor(k);
|
766 |
762 |
const double v0=M_E/(M_E-delta);
|
767 |
763 |
do {
|
768 |
764 |
double V0=1.0-real<double>();
|
769 |
765 |
double V1=1.0-real<double>();
|
770 |
766 |
double V2=1.0-real<double>();
|
771 |
767 |
if(V2<=v0)
|
772 |
768 |
{
|
773 |
769 |
xi=std::pow(V1,1.0/delta);
|
774 |
770 |
nu=V0*std::pow(xi,delta-1.0);
|
775 |
771 |
}
|
776 |
772 |
else
|
777 |
773 |
{
|
778 |
774 |
xi=1.0-std::log(V1);
|
779 |
775 |
nu=V0*std::exp(-xi);
|
780 |
776 |
}
|
781 |
777 |
} while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
|
782 |
778 |
return theta*(xi-gamma(int(std::floor(k))));
|
783 |
779 |
}
|
784 |
780 |
|
785 |
781 |
/// Weibull distribution
|
786 |
782 |
|
787 |
783 |
/// This function generates a Weibull distribution random number.
|
788 |
784 |
///
|
789 |
785 |
///\param k shape parameter (<tt>k>0</tt>)
|
790 |
786 |
///\param lambda scale parameter (<tt>lambda>0</tt>)
|
791 |
787 |
///
|
792 |
788 |
double weibull(double k,double lambda)
|
793 |
789 |
{
|
794 |
790 |
return lambda*pow(-std::log(1.0-real<double>()),1.0/k);
|
795 |
791 |
}
|
796 |
792 |
|
797 |
793 |
/// Pareto distribution
|
798 |
794 |
|
799 |
795 |
/// This function generates a Pareto distribution random number.
|
800 |
796 |
///
|
801 |
797 |
///\param k shape parameter (<tt>k>0</tt>)
|
802 |
798 |
///\param x_min location parameter (<tt>x_min>0</tt>)
|
803 |
799 |
///
|
804 |
800 |
double pareto(double k,double x_min)
|
805 |
801 |
{
|
806 |
802 |
return exponential(gamma(k,1.0/x_min));
|
807 |
803 |
}
|
808 |
804 |
|
809 |
805 |
///@}
|
810 |
806 |
|
811 |
807 |
///\name Two dimensional distributions
|
812 |
808 |
///
|
813 |
809 |
|
814 |
810 |
///@{
|
815 |
811 |
|
816 |
812 |
/// Uniform distribution on the full unit circle.
|
|
813 |
|
|
814 |
/// Uniform distribution on the full unit circle.
|
|
815 |
///
|
817 |
816 |
dim2::Point<double> disc()
|
818 |
817 |
{
|
819 |
818 |
double V1,V2;
|
820 |
819 |
do {
|
821 |
820 |
V1=2*real<double>()-1;
|
822 |
821 |
V2=2*real<double>()-1;
|
823 |
822 |
|
824 |
823 |
} while(V1*V1+V2*V2>=1);
|
825 |
824 |
return dim2::Point<double>(V1,V2);
|
826 |
825 |
}
|
827 |
826 |
/// A kind of two dimensional Gauss distribution
|
828 |
827 |
|
829 |
828 |
/// This function provides a turning symmetric two-dimensional distribution.
|
830 |
829 |
/// Both coordinates are of standard normal distribution, but they are not
|
831 |
830 |
/// independent.
|
832 |
831 |
///
|
833 |
832 |
/// \note The coordinates are the two random variables provided by
|
834 |
833 |
/// the Box-Muller method.
|
835 |
834 |
dim2::Point<double> gauss2()
|
836 |
835 |
{
|
837 |
836 |
double V1,V2,S;
|
838 |
837 |
do {
|
839 |
838 |
V1=2*real<double>()-1;
|
840 |
839 |
V2=2*real<double>()-1;
|
841 |
840 |
S=V1*V1+V2*V2;
|
842 |
841 |
} while(S>=1);
|
843 |
842 |
double W=std::sqrt(-2*std::log(S)/S);
|
844 |
843 |
return dim2::Point<double>(W*V1,W*V2);
|
845 |
844 |
}
|
846 |
845 |
/// A kind of two dimensional exponential distribution
|
847 |
846 |
|
848 |
847 |
/// This function provides a turning symmetric two-dimensional distribution.
|
849 |
848 |
/// The x-coordinate is of conditionally exponential distribution
|
850 |
849 |
/// with the condition that x is positive and y=0. If x is negative and
|
851 |
850 |
/// y=0 then, -x is of exponential distribution. The same is true for the
|
852 |
851 |
/// y-coordinate.
|
853 |
852 |
dim2::Point<double> exponential2()
|
854 |
853 |
{
|
855 |
854 |
double V1,V2,S;
|
856 |
855 |
do {
|
857 |
856 |
V1=2*real<double>()-1;
|
858 |
857 |
V2=2*real<double>()-1;
|
859 |
858 |
S=V1*V1+V2*V2;
|
860 |
859 |
} while(S>=1);
|
861 |
860 |
double W=-std::log(S)/S;
|
862 |
861 |
return dim2::Point<double>(W*V1,W*V2);
|
863 |
862 |
}
|
864 |
863 |
|
865 |
864 |
///@}
|
866 |
865 |
};
|
867 |
866 |
|
868 |
867 |
|
869 |
868 |
extern Random rnd;
|
870 |
869 |
|
871 |
870 |
}
|
872 |
871 |
|
873 |
872 |
#endif
|