cgma
RandomMersenne Class Reference

#include <RandomMersenne.hpp>

List of all members.

Public Member Functions

 RandomMersenne (unsigned long seed)
 constructor initialized random number generator, so it takes a seed
 RandomMersenne (unsigned long init_key[], unsigned long key_length)
 constructor initialized random number generator, so it takes a seed array
void init_by_array (unsigned long init_key[], unsigned long key_length)
unsigned long genrand_int32 ()
 generates a random number on [0,0xffffffff]-interval
long genrand_int31 ()
 generates a random number on [0,0x7fffffff]-interval
double genrand_real1 ()
 generates a random number on [0,1]-real-interval
double genrand_real2 ()
 generates a random number on [0,1)-real-interval
double genrand_real3 ()
 generates a random number on (0,1)-real-interval
double genrand_res53 ()
 generates a random number on [0,1) with 53-bit resolution

Private Types

enum  {
  N = 624, M = 397, MATRIX_A = 0x9908b0dfU, UPPER_MASK = 0x80000000U,
  LOWER_MASK = 0x7fffffffU
}

Private Member Functions

 RandomMersenne ()
 private constructor to prevent constructing without seed
void init_genrand (unsigned long s)
 initializes state[N] with a seed

Private Attributes

unsigned long mt [N]
unsigned mti
 the array for the state vector
int left
int initf
unsigned long * next

Detailed Description

Definition at line 43 of file RandomMersenne.hpp.


Member Enumeration Documentation

anonymous enum [private]
Enumerator:
N 
M 
MATRIX_A 
UPPER_MASK 

constant vector a

LOWER_MASK 

Definition at line 84 of file RandomMersenne.hpp.

       {
    N = 624, M = 397,
    MATRIX_A = 0x9908b0dfU, 
    UPPER_MASK = 0x80000000U, /* most significant w-r bits */
    LOWER_MASK = 0x7fffffffU /* least significant r bits */
  };

Constructor & Destructor Documentation

RandomMersenne::RandomMersenne ( unsigned long  seed) [inline]

constructor initialized random number generator, so it takes a seed

Definition at line 99 of file RandomMersenne.hpp.

{
  init_genrand(seed);
}
RandomMersenne::RandomMersenne ( unsigned long  init_key[],
unsigned long  key_length 
) [inline]

constructor initialized random number generator, so it takes a seed array

Definition at line 104 of file RandomMersenne.hpp.

{
  init_by_array(init_key, key_length);
}
RandomMersenne::RandomMersenne ( ) [inline, private]

private constructor to prevent constructing without seed

Definition at line 79 of file RandomMersenne.hpp.

{}

Member Function Documentation

long RandomMersenne::genrand_int31 ( ) [inline]

generates a random number on [0,0x7fffffff]-interval

Definition at line 111 of file RandomMersenne.hpp.

{
  return (long) (genrand_int32() >> 1);
}
unsigned long RandomMersenne::genrand_int32 ( )

generates a random number on [0,0xffffffff]-interval

Definition at line 97 of file RandomMersenne.cpp.

{
  unsigned long y;
  static unsigned long mag01[2]={0x0UL, MATRIX_A};
    /* mag01[x] = x * MATRIX_A  for x=0,1 */

  if (mti >= N) { /* generate N words at one time */
    unsigned kk;

      //if (mti == N+1)   /* if init_genrand() has not been called, */
      //init_genrand(5489UL); /* a default initial seed is used */

    for (kk=0;kk<N-M;kk++) {
      y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
      mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
    }
    for (;kk<N-1;kk++) {
      y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
      mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
    }
    y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
    mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];

    mti = 0;
  }
  
  y = mt[mti++];

    /* Tempering */
  y ^= (y >> 11);
  y ^= (y << 7) & 0x9d2c5680UL;
  y ^= (y << 15) & 0xefc60000UL;
  y ^= (y >> 18);

  return y;
}

generates a random number on [0,1]-real-interval

generates a random number on [0,1)-real-interval

generates a random number on (0,1)-real-interval

generates a random number on [0,1) with 53-bit resolution

void RandomMersenne::init_by_array ( unsigned long  init_key[],
unsigned long  key_length 
)

initialize by an array with array-length init_key is the array for initializing keys key_length is its length

Definition at line 71 of file RandomMersenne.cpp.

{
  unsigned long i, j, k;
  const unsigned long n = (unsigned long)N;
  init_genrand(19650218UL);
  i=1UL; j=0UL;
  k = (n>key_length ? n : key_length);
  for (; k; k--) {
    mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
      + init_key[j] + j; /* non linear */
    mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
    i++; j++;
    if (i>=n) { mt[0] = mt[n-1]; i=1; }
    if (j>=key_length) j=0;
  }
  for (k=n-1; k; k--) {
    mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
      - i; /* non linear */
    mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
    i++;
    if (i>=n) { mt[0] = mt[n-1]; i=1; }
  }

  mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
}
void RandomMersenne::init_genrand ( unsigned long  s) [private]

initializes state[N] with a seed

Definition at line 53 of file RandomMersenne.cpp.

{
  mt[0]= s & 0xffffffffUL;
  for (mti=1; mti<N; mti++) {
    mt[mti] = 
      (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
      /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
      /* In the previous versions, MSBs of the seed affect   */
      /* only MSBs of the array mt[].                        */
      /* 2002/01/09 modified by Makoto Matsumoto             */
    mt[mti] &= 0xffffffffUL;
      /* for >32 bit machines */
  }
}

Member Data Documentation

int RandomMersenne::initf [private]

Definition at line 94 of file RandomMersenne.hpp.

int RandomMersenne::left [private]

Definition at line 93 of file RandomMersenne.hpp.

unsigned long RandomMersenne::mt[N] [private]

Definition at line 91 of file RandomMersenne.hpp.

unsigned RandomMersenne::mti [private]

the array for the state vector

Definition at line 92 of file RandomMersenne.hpp.

unsigned long* RandomMersenne::next [private]

Definition at line 95 of file RandomMersenne.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines