1 // -*- C++ -*- 2 3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the terms 7 // of the GNU General Public License as published by the Free Software 8 // Foundation; either version 3, or (at your option) any later 9 // version. 10 11 // This library is distributed in the hope that it will be useful, but 12 // WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 // General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /** @file parallel/random_number.h 26 * @brief Random number generator based on the Mersenne twister. 27 * This file is a GNU parallel extension to the Standard C++ Library. 28 */ 29 30 // Written by Johannes Singler. 31 32 #ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 33 #define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1 34 35 #include <parallel/types.h> 36 #include <tr1/random> 37 38 namespace __gnu_parallel 39 { 40 /** @brief Random number generator, based on the Mersenne twister. */ 41 class random_number 42 { 43 private: 44 std::tr1::mt19937 mt; 45 uint64 supremum; 46 uint64 RAND_SUP; 47 double supremum_reciprocal; 48 double RAND_SUP_REC; 49 50 // Assumed to be twice as long as the usual random number. 51 uint64 cache; 52 53 // Bit results. 54 int bits_left; 55 56 static uint32 57 scale_down(uint64 x, 58 #if _GLIBCXX_SCALE_DOWN_FPU 59 uint64 /*supremum*/, double supremum_reciprocal) 60 #else 61 uint64 supremum, double /*supremum_reciprocal*/) 62 #endif 63 { 64 #if _GLIBCXX_SCALE_DOWN_FPU 65 return uint32(x * supremum_reciprocal); 66 #else 67 return static_cast<uint32>(x % supremum); 68 #endif 69 } 70 71 public: 72 /** @brief Default constructor. Seed with 0. */ 73 random_number() 74 : mt(0), supremum(0x100000000ULL), 75 RAND_SUP(1ULL << (sizeof(uint32) * 8)), 76 supremum_reciprocal(double(supremum) / double(RAND_SUP)), 77 RAND_SUP_REC(1.0 / double(RAND_SUP)), 78 cache(0), bits_left(0) { } 79 80 /** @brief Constructor. 81 * @param seed Random seed. 82 * @param supremum Generate integer random numbers in the 83 * interval @c [0,supremum). */ 84 random_number(uint32 seed, uint64 supremum = 0x100000000ULL) 85 : mt(seed), supremum(supremum), 86 RAND_SUP(1ULL << (sizeof(uint32) * 8)), 87 supremum_reciprocal(double(supremum) / double(RAND_SUP)), 88 RAND_SUP_REC(1.0 / double(RAND_SUP)), 89 cache(0), bits_left(0) { } 90 91 /** @brief Generate unsigned random 32-bit integer. */ 92 uint32 93 operator()() 94 { return scale_down(mt(), supremum, supremum_reciprocal); } 95 96 /** @brief Generate unsigned random 32-bit integer in the 97 interval @c [0,local_supremum). */ 98 uint32 99 operator()(uint64 local_supremum) 100 { 101 return scale_down(mt(), local_supremum, 102 double(local_supremum * RAND_SUP_REC)); 103 } 104 105 /** @brief Generate a number of random bits, run-time parameter. 106 * @param bits Number of bits to generate. */ 107 unsigned long 108 genrand_bits(int bits) 109 { 110 unsigned long res = cache & ((1 << bits) - 1); 111 cache = cache >> bits; 112 bits_left -= bits; 113 if (bits_left < 32) 114 { 115 cache |= ((uint64(mt())) << bits_left); 116 bits_left += 32; 117 } 118 return res; 119 } 120 }; 121 122 } // namespace __gnu_parallel 123 124 #endif /* _GLIBCXX_PARALLEL_RANDOM_NUMBER_H */ 125