23 #ifndef WFMATH_MERSENNE_TWISTER_H_
24 #define WFMATH_MERSENNE_TWISTER_H_
35 typedef uint32_t uint32;
37 static const uint32 state_size = 624;
41 explicit MTRand(uint32 oneSeed);
42 explicit MTRand(
const uint32 bigSeed[], uint32 seedLength = state_size);
45 template<
typename FloatT>
48 double rand(
const double& n);
52 uint32 randInt(uint32 n);
55 void seed(uint32 oneSeed);
56 void seed(
const uint32 init_vector[], uint32 init_vector_length = state_size);
58 std::ostream& save(std::ostream&)
const;
59 std::istream& load(std::istream&);
64 uint32 state[state_size];
69 inline MTRand::MTRand(uint32 oneSeed)
73 inline MTRand::MTRand(
const uint32 bigSeed[],
const uint32 seedLength)
75 { seed(bigSeed, seedLength); }
77 inline MTRand::MTRand()
82 inline float MTRand::rand<float>()
83 {
return float(randInt()) * (1.0f/4294967295.0f); }
86 inline double MTRand::rand<double>()
87 {
return double(randInt()) * (1.0/4294967295.0); }
89 inline double MTRand::rand()
90 {
return double(randInt()) * (1.0/4294967295.0); }
92 inline double MTRand::rand(
const double& n )
93 {
return rand() * n; }
96 inline MTRand::uint32 MTRand::randInt(uint32 n)
107 i = randInt() & used;
114 inline void MTRand::save(uint32* saveArray)
const
116 uint32 *sa = saveArray;
117 const uint32 *s = state;
119 for( ; i--; *sa++ = *s++ ) {}
124 inline void MTRand::load(uint32 *
const loadArray)
127 uint32 *la = loadArray;
129 for( ; i--; *s++ = *la++ ) {}
131 pNext = &state[state_size-left];
135 std::ostream& operator<<(std::ostream& os, MTRand
const& mtrand);
136 std::istream& operator>>(std::istream& is, MTRand& mtrand);
Generic library namespace.