23 #ifndef WFMATH_MERSENNE_TWISTER_H_
24 #define WFMATH_MERSENNE_TWISTER_H_
33 typedef uint32_t uint32;
35 static const uint32 state_size = 624;
39 explicit MTRand(uint32 oneSeed);
40 explicit MTRand(uint32 bigSeed[], uint32 seedLength = state_size);
43 template<
typename FloatT>
46 double rand(
const double& n);
50 uint32 randInt(uint32 n);
53 void seed(uint32 oneSeed);
54 void seed(
const uint32 init_vector[], uint32 init_vector_length = state_size);
56 std::ostream& save(std::ostream&)
const;
57 std::istream& load(std::istream&);
62 uint32 state[state_size];
67 inline MTRand::MTRand(uint32 oneSeed)
71 inline MTRand::MTRand(uint32 bigSeed[], uint32 seedLength)
73 { seed(bigSeed, seedLength); }
75 inline MTRand::MTRand()
80 inline float MTRand::rand<float>()
81 {
return float(randInt()) * (1.0f/4294967295.0f); }
84 inline double MTRand::rand<double>()
85 {
return double(randInt()) * (1.0/4294967295.0); }
87 inline double MTRand::rand()
88 {
return double(randInt()) * (1.0/4294967295.0); }
90 inline double MTRand::rand(
const double& n )
91 {
return rand() * n; }
94 inline MTRand::uint32 MTRand::randInt(uint32 n)
105 i = randInt() & used;
112 inline void MTRand::save(uint32* saveArray)
const
114 uint32 *sa = saveArray;
115 const uint32 *s = state;
117 for( ; i--; *sa++ = *s++ ) {}
122 inline void MTRand::load(uint32 *
const loadArray)
125 uint32 *la = loadArray;
127 for( ; i--; *s++ = *la++ ) {}
129 pNext = &state[state_size-left];
133 std::ostream& operator<<(std::ostream& os, MTRand
const& mtrand);
134 std::istream& operator>>(std::istream& is, MTRand& mtrand);
Generic library namespace.