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