A reduced and less complicated version.
- Removed that silly idea of storing the last random generated and thus simplified the class usage considerably.
- Replaced high() and low() with max() and min(). Didn't know what I was thinking...
- Sizeof still reports 2508 bytes. But that is the price to pay for a Mersenne Twister, I guess.
- The more I work on this, the less I'm convinced a class exclusively meant to produce random numbers using only one method is a useful thing.
I do need someone's opinion on the way I'm implementing the uniform distribution method on CRand::generate(). I have this idea I'm totally missing the point. Using a Mersenne Twister and then use the result only to seed a rand() call makes little sense.
PHP Code:
unsigned long CRand::generate(Distribution dist) {
double uniform_deviate;
/* ... Mersenne Twister implemented here ...*/
// last stages of Mersenne Twister
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
/* set within range with chosen distribution */
switch(dist) {
case None:
y = min_ + y % ( max_ + 1 - min_ );
break;
case Uniform_Deviation:
srand(static_cast<int>(y));
uniform_deviate = rand() * ( 1.0 / ( RAND_MAX + 1.0 ) );
y = static_cast<unsigned long>( min_ + uniform_deviate * (max_ + 1 - min_) );
break;
}
return y;
}