#ifndef MIDDLE_CRAND_H
#define MIDDLE_CRAND_H
/*
Random number Class
- Wrapped around Julienne Walker's implementation of Mersenne Twister's found at
http://eternallyconfuzzled.com/brain.html, as of July, 18, 2005
- Slightly altered to accomodate the class implementation
Constructor:
- a pair of unsigned longs establishing an inclusive range
Interface:
Each instance of the CRand class represents an object that can be used to generate
random integrals within the range provided by the constructor. When initialized, the
object will seed itself. It is possible to reseed the object through seed_again().
Number generation is done through generate(). Currently two distribution methods are
supported through a enumeration, None and Uniform Deviation.
The range can be changed and checked through the overloaded min() and max()
functions. CRange automatically fixes the range if a min() is provided that is
higher than max() (or the opposite, a max() is provided that is lower than the
current min()), by swapping the values. This range checking is also done in the
constructor.
Usage Example:
#include
#include
#include "crand.h"
int main()
{
CRand test(0,3); // creates an object test. Random numbers are between 0 and 3
// run 20 times. Each time get a new random number between 0 and 3 using a
// uniform deviation based distribution.
for(int i = 0; i != 20; ++i) {
std::cout << i + 1 << ": " ;
std::cout << test.generate(CRand::Uniform_Deviation);
std::cout << std::endl;
}
return EXIT_SUCCESS;
}
*/
class CRand {
private:
static unsigned int time_seed();
static const int N = 624;
static const int M = 397;
static const unsigned long A = 0x9908b0dfUL;
static const unsigned long U = 0x80000000UL;
static const unsigned long L = 0x7fffffffUL;
public:
enum Distribution { None, Uniform_Deviation };
explicit CRand(unsigned long, unsigned long);
void seed_again(unsigned int = time_seed());
unsigned long generate(Distribution = None);
unsigned long max() const { return max_; }
unsigned long min() const { return min_; }
void max(unsigned long);
void min(unsigned long);
private:
unsigned long min_;
unsigned long max_;
// internal state (per object)
unsigned long x[N];
int next;
};
#endif // MIDDLE_CRAND_H