mtrand.cpp
Go to the documentation of this file.
1 // mtrand.cpp, see include file mtrand.h for information
2 
3 #include "mtrand.h"
4 // non-inline function definitions and static member definitions cannot
5 // reside in header file because of the risk of multiple declarations
6 
7 // initialization of static private members
8 unsigned long MTRand_int32::state[n] = {0x0UL};
9 int MTRand_int32::p = 0;
10 bool MTRand_int32::init = false;
11 
12 void MTRand_int32::gen_state() { // generate new state vector
13  for (int i = 0; i < (n - m); ++i)
14  state[i] = state[i + m] ^ twiddle(state[i], state[i + 1]);
15  for (int i = n - m; i < (n - 1); ++i)
16  state[i] = state[i + m - n] ^ twiddle(state[i], state[i + 1]);
17  state[n - 1] = state[m - 1] ^ twiddle(state[n - 1], state[0]);
18  p = 0; // reset position
19 }
20 
21 void MTRand_int32::seed(unsigned long s) { // init by 32 bit seed
22  state[0] = s & 0xFFFFFFFFUL; // for > 32 bit machines
23  for (int i = 1; i < n; ++i) {
24  state[i] = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i;
25 // see Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier
26 // in the previous versions, MSBs of the seed affect only MSBs of the array state
27 // 2002/01/09 modified by Makoto Matsumoto
28  state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
29  }
30  p = n; // force gen_state() to be called for next random number
31 }
32 
33 void MTRand_int32::seed(const unsigned long* array, int size) { // init by array
34  seed(19650218UL);
35  int i = 1, j = 0;
36  for (int k = ((n > size) ? n : size); k; --k) {
37  state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL))
38  + array[j] + j; // non linear
39  state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
40  ++j; j %= size;
41  if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
42  }
43  for (int k = n - 1; k; --k) {
44  state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i;
45  state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
46  if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
47  }
48  state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
49  p = n; // force gen_state() to be called for next random number
50 }
static bool init
Definition: mtrand.h:70
void gen_state()
Definition: mtrand.cpp:12
unsigned long twiddle(unsigned long, unsigned long)
Definition: mtrand.h:80
static unsigned long state[n]
Definition: mtrand.h:68
static int p
Definition: mtrand.h:69
void seed(unsigned long)
Definition: mtrand.cpp:21
static const int m
Definition: mtrand.h:66
static const int n
Definition: mtrand.h:66


rrt_exploration
Author(s): Hassan Umari
autogenerated on Mon Jun 10 2019 14:57:45