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 MTRand(uint32 oneSeed);
42 MTRand(uint32*
const bigSeed, uint32
const 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(uint32*
const init_vector, uint32 init_vector_length = state_size);
58 std::ostream& save(std::ostream&)
const;
59 std::istream& load(std::istream&);
61 static MTRand instance;
64 uint32 state[state_size];
69 inline MTRand::MTRand(uint32 s)
73 inline MTRand::MTRand(uint32*
const 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 register uint32 *sa = saveArray;
117 register const uint32 *s = state;
118 register int i = state_size;
119 for( ; i--; *sa++ = *s++ ) {}
124 inline void MTRand::load(uint32 *
const loadArray)
126 register uint32 *s = state;
127 register uint32 *la = loadArray;
128 register int i = state_size;
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);
140 #endif // WFMATH_MERSENNE_TWISTER_H_ Generic library namespace.
Definition: atlasconv.h:45