mtrand.cpp
Go to the documentation of this file.
00001 // mtrand.cpp, see include file mtrand.h for information
00002 
00003 #include "mtrand.h"
00004 // non-inline function definitions and static member definitions cannot
00005 // reside in header file because of the risk of multiple declarations
00006 
00007 // initialization of static private members
00008 unsigned long MTRand_int32::state[n] = {0x0UL};
00009 int MTRand_int32::p = 0;
00010 bool MTRand_int32::init = false;
00011 
00012 void MTRand_int32::gen_state() { // generate new state vector
00013   for (int i = 0; i < (n - m); ++i)
00014     state[i] = state[i + m] ^ twiddle(state[i], state[i + 1]);
00015   for (int i = n - m; i < (n - 1); ++i)
00016     state[i] = state[i + m - n] ^ twiddle(state[i], state[i + 1]);
00017   state[n - 1] = state[m - 1] ^ twiddle(state[n - 1], state[0]);
00018   p = 0; // reset position
00019 }
00020 
00021 void MTRand_int32::seed(unsigned long s) {  // init by 32 bit seed
00022   state[0] = s & 0xFFFFFFFFUL; // for > 32 bit machines
00023   for (int i = 1; i < n; ++i) {
00024     state[i] = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i;
00025 // see Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier
00026 // in the previous versions, MSBs of the seed affect only MSBs of the array state
00027 // 2002/01/09 modified by Makoto Matsumoto
00028     state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
00029   }
00030   p = n; // force gen_state() to be called for next random number
00031 }
00032 
00033 void MTRand_int32::seed(const unsigned long* array, int size) { // init by array
00034   seed(19650218UL);
00035   int i = 1, j = 0;
00036   for (int k = ((n > size) ? n : size); k; --k) {
00037     state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL))
00038       + array[j] + j; // non linear
00039     state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
00040     ++j; j %= size;
00041     if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
00042   }
00043   for (int k = n - 1; k; --k) {
00044     state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i;
00045     state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
00046     if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
00047   }
00048   state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
00049   p = n; // force gen_state() to be called for next random number
00050 }


rrt_exploration
Author(s): Hassan Umari
autogenerated on Thu Jun 6 2019 20:54:03