Minor change in 'make rpm' behaviour.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_RANDOM_H
20 #define LEMON_RANDOM_H
30 ///\brief Mersenne Twister random number generator
32 ///\author Balazs Dezso
40 /// \brief Mersenne Twister random number generator
42 /// The Mersenne Twister is a twisted generalized feedback
43 /// shift-register generator of Matsumoto and Nishimura. The period of
44 /// this generator is \f$ 2^{19937} - 1 \f$ and it is equi-distributed in
45 /// 623 dimensions. The time performance of this generator is comparable
46 /// to the commonly used generators.
48 /// \author Balazs Dezso
51 static const int length = 624;
52 static const int shift = 397;
56 static const unsigned long min = 0ul;
57 static const unsigned long max = ~0ul;
59 /// \brief Constructor
61 /// Constructor with time dependent seeding.
62 Random() { initState(std::time(0)); }
64 /// \brief Constructor
67 Random(unsigned long seed) { initState(seed); }
69 /// \brief Copy constructor
71 /// Copy constructor. The generated sequence will be identical to
72 /// the other sequence.
73 Random(const Random& other) {
74 std::copy(other.state, other.state + length, state);
75 current = state + (other.current - other.state);
78 /// \brief Assign operator
80 /// Assign operator. The generated sequence will be identical to
81 /// the other sequence.
82 Random& operator=(const Random& other) {
84 std::copy(other.state, other.state + length, state);
85 current = state + (other.current - other.state);
90 /// \brief Returns an unsigned random number
92 /// It returns an unsigned integer random number from the range
93 /// \f$ \{0, 1, \dots, 2^{32} - 1\} \f$.
94 unsigned long getUnsigned() {
95 if (current == state) fillState();
97 unsigned long rnd = *current;
99 rnd ^= (rnd << 7) & 0x9D2C5680ul;
100 rnd ^= (rnd << 15) & 0xEFC60000ul;
105 /// \brief Returns a random number
107 /// It returns an integer random number from the range
108 /// \f$ \{-2^{31}, \dots, 2^{31} - 1\} \f$.
110 return (long)getUnsigned();
113 /// \brief Returns an unsigned integer random number
115 /// It returns an unsigned integer random number from the range
116 /// \f$ \{0, 1, \dots, 2^{31} - 1\} \f$.
118 return (long)(getUnsigned() >> 1);
121 /// \brief Returns a random bool
123 /// It returns a random bool.
125 return (bool)(getUnsigned() & 1);
128 /// \brief Returns a real random number
130 /// It returns a real random number from the range
131 /// \f$ [0, 1) \f$. The double will have 32 significant bits.
133 return std::ldexp((double)getUnsigned(), -32);
136 /// \brief Returns a real random number
138 /// It returns a real random number from the range
139 /// \f$ [0, 1) \f$. The double will have 53 significant bits,
140 /// but it is slower than the \c getReal().
141 double getPrecReal() {
142 return std::ldexp((double)(getUnsigned() >> 5), -27) +
143 std::ldexp((double)(getUnsigned() >> 6), -53);
146 /// \brief Returns an unsigned random number from a given range
148 /// It returns an unsigned integer random number from the range
149 /// \f$ \{0, 1, \dots, n - 1\} \f$.
150 unsigned long getUnsigned(unsigned long n) {
151 unsigned long mask = n - 1, rnd;
157 do rnd = getUnsigned() & mask; while (rnd >= n);
161 /// \brief Returns a random number from a given range
163 /// It returns an unsigned integer random number from the range
164 /// \f$ \{0, 1, \dots, n - 1\} \f$.
165 long getInt(long n) {
166 long mask = n - 1, rnd;
172 do rnd = getUnsigned() & mask; while (rnd >= n);
178 void initState(unsigned long seed) {
179 static const unsigned long mul = 0x6c078965ul;
183 unsigned long *curr = state + length - 1;
184 curr[0] = seed; --curr;
185 for (int i = 1; i < length; ++i) {
186 curr[0] = (mul * ( curr[1] ^ (curr[1] >> 30) ) + i);
192 static const unsigned long mask[2] = { 0x0ul, 0x9908B0DFul };
193 static const unsigned long loMask = (1ul << 31) - 1;
194 static const unsigned long hiMask = ~loMask;
196 current = state + length;
198 register unsigned long *curr = state + length - 1;
201 num = length - shift;
203 curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
204 curr[- shift] ^ mask[curr[-1] & 1ul];
209 curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
210 curr[length - shift] ^ mask[curr[-1] & 1ul];
213 curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
214 curr[length - shift] ^ mask[curr[length - 1] & 1ul];
218 unsigned long *current;
219 unsigned long state[length];
227 /// \brief Mersenne Twister random number generator
229 /// The Mersenne Twister is a twisted generalized feedback
230 /// shift-register generator of Matsumoto and Nishimura. The period of
231 /// this generator is \f$ 2^{19937} - 1 \f$ and it is equi-distributed in
232 /// 311 dimensions. The time performance of this generator is comparable
233 /// to the commonly used generators.
236 static const int length = 312;
237 static const int shift = 156;
241 static const unsigned long min = 0ul;
242 static const unsigned long max = ~0ul;
244 /// \brief Constructor
246 /// Constructor with time dependent seeding.
247 Random() { initState(std::time(0)); }
249 /// \brief Constructor
252 Random(unsigned long seed) { initState(seed); }
254 /// \brief Copy constructor
256 /// Copy constructor. The generated sequence will be identical to
257 /// the other sequence.
258 Random(const Random& other) {
259 std::copy(other.state, other.state + length, state);
260 current = state + (other.current - other.state);
263 /// \brief Assign operator
265 /// Assign operator. The generated sequence will be identical to
266 /// the other sequence.
267 Random& operator=(const Random& other) {
268 if (&other != this) {
269 std::copy(other.state, other.state + length, state);
270 current = state + (other.current - other.state);
275 /// \brief Returns an unsigned random number
277 /// It returns an unsigned integer random number from the range
278 /// \f$ \{0, 1, \dots, 2^{64} - 1\} \f$.
279 unsigned long getUnsigned() {
280 if (current == state) fillState();
282 unsigned long rnd = *current;
283 rnd ^= (rnd >> 29) & 0x5555555555555555ul;
284 rnd ^= (rnd << 17) & 0x71D67FFFEDA60000ul;
285 rnd ^= (rnd << 37) & 0xFFF7EEE000000000ul;
290 /// \brief Returns a random number
292 /// It returns an integer random number from the range
293 /// \f$ \{-2^{63}, \dots, 2^{63} - 1\} \f$.
295 return (long)getUnsigned();
298 /// \brief Returns an unsigned integer random number
300 /// It returns an unsigned integer random number from the range
301 /// \f$ \{0, 1, \dots, 2^{63} - 1\} \f$.
303 return (long)(getUnsigned() >> 1);
306 /// \brief Returns a random bool
308 /// It returns a random bool.
310 return (bool)(getUnsigned() & 1);
313 /// \brief Returns a real random number
315 /// It returns a real random number from the range
318 return std::ldexp((double)(getUnsigned() >> 11), -53);
321 /// \brief Returns a real random number
323 /// It returns a real random number from the range
324 /// \f$ [0, 1) \f$. This function is identical to the \c getReal().
325 double getPrecReal() {
329 /// \brief Returns an unsigned random number from a given range
331 /// It returns an unsigned integer random number from the range
332 /// \f$ \{0, 1, \dots, n - 1\} \f$.
333 unsigned long getUnsigned(unsigned long n) {
334 unsigned long mask = n - 1, rnd;
341 do rnd = getUnsigned() & mask; while (rnd >= n);
345 /// \brief Returns a random number from a given range
347 /// It returns an unsigned integer random number from the range
348 /// \f$ \{0, 1, \dots, n - 1\} \f$.
349 long getInt(long n) {
350 long mask = n - 1, rnd;
357 do rnd = getUnsigned() & mask; while (rnd >= n);
363 void initState(unsigned long seed) {
365 static const unsigned long mul = 0x5851F42D4C957F2Dul;
369 unsigned long *curr = state + length - 1;
370 curr[0] = seed; --curr;
371 for (int i = 1; i < length; ++i) {
372 curr[0] = (mul * ( curr[1] ^ (curr[1] >> 62) ) + i);
378 static const unsigned long mask[2] = { 0x0ul, 0xB5026F5AA96619E9ul };
379 static const unsigned long loMask = (1ul << 31) - 1;
380 static const unsigned long hiMask = ~loMask;
382 current = state + length;
384 register unsigned long *curr = state + length - 1;
387 num = length - shift;
389 curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
390 curr[- shift] ^ mask[curr[-1] & 1ul];
395 curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
396 curr[length - shift] ^ mask[curr[-1] & 1ul];
399 curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
400 curr[length - shift] ^ mask[curr[length - 1] & 1ul];
404 unsigned long *current;
405 unsigned long state[length];
413 /// \brief Mersenne Twister random number generator
415 /// The Mersenne Twister is a twisted generalized feedback
416 /// shift-register generator of Matsumoto and Nishimura. There is
417 /// two different implementation in the Lemon library, one for
418 /// 32-bit processors and one for 64-bit processors. The period of
419 /// the generated sequence is \f$ 2^{19937} - 1 \f$, the generated
420 /// sequence of 32-bit random numbers is equi-distributed in 623
421 /// dimensions. The time performance of this generator is comparable
422 /// to the commonly used generators.
426 static const unsigned long min = 0ul;
427 static const unsigned long max = ~0ul;
429 /// \brief Constructor
431 /// Constructor with time dependent seeding.
434 /// \brief Constructor
437 Random(unsigned long seed) {}
439 /// \brief Copy constructor
441 /// Copy constructor. The generated sequence will be identical to
442 /// the other sequence.
443 Random(const Random& other) {}
445 /// \brief Assign operator
447 /// Assign operator. The generated sequence will be identical to
448 /// the other sequence.
449 Random& operator=(const Random& other) { return *this; }
451 /// \brief Returns an unsigned random number
453 /// It returns an unsigned integer random number from the range
454 /// \f$ \{0, 1, \dots, 2^{64} - 1\} \f$ for 64-bit processors and from
455 /// \f$ \{0, 1, \dots, 2^{32} - 1\} \f$ for 32-bit processors.
456 unsigned long getUnsigned() { return 0ul; }
458 /// \brief Returns a random number
460 /// It returns an integer random number from the range
461 /// \f$ \{-2^{63}, \dots, 2^{63} - 1\} \f$ for 64-bit processors and from
462 /// \f$ \{-2^{31}, \dots, 2^{31} - 1\} \f$ for 32-bit processors.
463 long getInt() { return 0l; }
465 /// \brief Returns an unsigned integer random number
467 /// It returns an unsigned integer random number from the range
468 /// \f$ \{0, 1, \dots, 2^{63} - 1\} \f$ for 64-bit processors and
469 /// from \f$ \{0, 1, \dots, 2^{31} - 1\} \f$ for 32-bit processors.
470 long getNatural() { return 0l; }
472 /// \brief Returns a random bool
474 /// It returns a random bool.
475 bool getBool() { return false; }
477 /// \brief Returns a real random number
479 /// It returns a real random number from the range
480 /// \f$ [0, 1) \f$. For 32-bit processors the generated random
481 /// number will just have 32 significant bits.
482 double getReal() { return 0.0; }
484 /// \brief Returns a real random number
486 /// It returns a real random number from the range
487 /// \f$ [0, 1) \f$. This function returns random numbers with 53
488 /// significant bits for 32-bit processors. For 64-bit processors
489 /// it is identical to the \c getReal().
490 double getPrecReal() { return 0.0; }
492 /// \brief Returns an unsigned random number from a given range
494 /// It returns an unsigned integer random number from the range
495 /// \f$ \{0, 1, \dots, n - 1\} \f$.
496 unsigned long getUnsigned(unsigned long n) { return 0; }
498 /// \brief Returns a random number from a given range
500 /// It returns an unsigned integer random number from the range
501 /// \f$ \{0, 1, \dots, n - 1\} \f$.
502 long getInt(long n) { return 0; }
508 /// \brief Global random number generator instance
510 /// A global mersenne twister random number generator instance