
-----------------------------------
md
Mon Apr 24, 2006 10:28 pm

Mersenne Twister
-----------------------------------
I've grown fedup with the very poor random number generators in C/C++; so I wrote this simple routine to get much better random numbers. Implements the Mersenne Twister ;)


/*
	The Mersenne Twister

	The Mersenne Twister is a random number generator, invented/discovered in 1996 by Matsumora and Nishimura. 
	MT is a twisted GFSR(624,397), similar in spirit to R250 and in speed to R250. MT has an period of 2^19937-1.
	It is a very good random number generator.

*/

#include 
#include 
#include 

const int MT_LEN = 624;

int mt_index;
unsigned long mt_buffer

Code is also on svn://svn.nxor.org/public/mt_rng

-----------------------------------
wtd
Mon Apr 24, 2006 11:59 pm


-----------------------------------
What about refactoring this such that a random number generator is an object that automatically inits when its constructor is called?

-----------------------------------
md
Tue Apr 25, 2006 9:26 am


-----------------------------------
Good idea! That'll be for tonight :D

-----------------------------------
wtd
Tue Apr 25, 2006 10:17 am


-----------------------------------
class mt_rang
{
   // ...
   
   template 
   mt_rng& operator>>(t& dest)
   {
      // get next random number, cast it to t,
      // and store in dest

      return *this;
   }
};

Then:

generator >> i >> j;

-----------------------------------
md
Tue Apr 25, 2006 7:27 pm


-----------------------------------
New code just for you wtd :P

cMTRandom.h


/*
	The Mersenne Twister

	The Mersenne Twister is a random number generator, invented/discovered in 1996 by Matsumora and Nishimura. 
	MT is a twisted GFSR(624,397), similar in spirit to R250 and in speed to R250. 	MT has an period of 2^19937-1.
	It is a very good random number generator.

*/

#ifndef cmtrandom_include
#define cmtrandom_include

class cMTRandom
{
public:
    cMTRandom(void);                // no seed
    cMTRandom(unsigned long seed);  // seeded (but why?!)
    
    unsigned long Rand(void);
    
    template  inline T& operator=(cMTRandom& boogie) { return static_cast (boogie.Rand()); }
    template  inline cMTRandom& operator>>(T& boogie) { boogie = static_cast(Rand()); return *this; }
    
protected:
    int index;
    unsigned long MT

cMTRandom.cpp

/*
	The Mersenne Twister

	The Mersenne Twister is a random number generator, invented/discovered in 1996 by Matsumora and Nishimura. 
	MT is a twisted GFSR(624,397), similar in spirit to R250 and in speed to R250. 	MT has an period of 2^19937-1.
	It is a very good random number generator.

*/

#include 
#include 
#include 
#include "cMTRandom.h"

/*
    Constants
*/

const unsigned long UPPER_MASK  = 0x80000000;
const unsigned long LOWER_MASK  = 0x7FFFFFFF;
const unsigned long MATRIX      = 0x9908B0DF;
const unsigned long MANGLE_A    = 0x9D2C5680;
const unsigned long MANGLE_B    = 0xEFC60000;

/*
    Variables
*/

cMTRandom::cMTRandom(void)
{
    /*
        On linux we can use /dev/urandom to get our starting seed, as it's
        less predictable then time() or srand()/rand() seeded from a specific 
        time. 
                
        On other systems just use time() since it's just as random as srand()/
        rand() seeded at time(), and a quicker.
    */

  #ifdef __linux__
    std::ifstream entropy("/dev/urandom", std::ios::in | std::ios::binary);
    entropy.read((char*)(&MT
