random_number.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H
00039 #define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1
00040
00041 #include <parallel/types.h>
00042 #include <tr1/random>
00043
00044 namespace __gnu_parallel
00045 {
00046
00047 class random_number
00048 {
00049 private:
00050 std::tr1::mt19937 mt;
00051 uint64 supremum;
00052 uint64 RAND_SUP;
00053 double supremum_reciprocal;
00054 double RAND_SUP_REC;
00055
00056
00057 uint64 cache;
00058
00059
00060 int bits_left;
00061
00062 static uint32
00063 scale_down(uint64 x,
00064 #if _GLIBCXX_SCALE_DOWN_FPU
00065 uint64 , double supremum_reciprocal)
00066 #else
00067 uint64 supremum, double )
00068 #endif
00069 {
00070 #if _GLIBCXX_SCALE_DOWN_FPU
00071 return uint32(x * supremum_reciprocal);
00072 #else
00073 return static_cast<uint32>(x % supremum);
00074 #endif
00075 }
00076
00077 public:
00078
00079 random_number()
00080 : mt(0), supremum(0x100000000ULL),
00081 RAND_SUP(1ULL << (sizeof(uint32) * 8)),
00082 supremum_reciprocal(double(supremum) / double(RAND_SUP)),
00083 RAND_SUP_REC(1.0 / double(RAND_SUP)),
00084 cache(0), bits_left(0) { }
00085
00086
00087
00088
00089
00090 random_number(uint32 seed, uint64 supremum = 0x100000000ULL)
00091 : mt(seed), supremum(supremum),
00092 RAND_SUP(1ULL << (sizeof(uint32) * 8)),
00093 supremum_reciprocal(double(supremum) / double(RAND_SUP)),
00094 RAND_SUP_REC(1.0 / double(RAND_SUP)),
00095 cache(0), bits_left(0) { }
00096
00097
00098 uint32
00099 operator()()
00100 { return scale_down(mt(), supremum, supremum_reciprocal); }
00101
00102
00103
00104 uint32
00105 operator()(uint64 local_supremum)
00106 {
00107 return scale_down(mt(), local_supremum,
00108 double(local_supremum * RAND_SUP_REC));
00109 }
00110
00111
00112
00113 unsigned long
00114 genrand_bits(int bits)
00115 {
00116 unsigned long res = cache & ((1 << bits) - 1);
00117 cache = cache >> bits;
00118 bits_left -= bits;
00119 if (bits_left < 32)
00120 {
00121 cache |= ((uint64(mt())) << bits_left);
00122 bits_left += 32;
00123 }
00124 return res;
00125 }
00126 };
00127
00128 }
00129
00130 #endif