Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vs_random.h
Go to the documentation of this file.
1 /*
2  * A C-program for MT19937, with initialization improved 2002/1/26.
3  * Coded by Takuji Nishimura and Makoto Matsumoto.
4  * Before using, initialize the state by using init_genrand(seed)
5  * or init_by_array(init_key, key_length).
6  * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
7  * All rights reserved.
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. The names of its contributors may not be used to endorse or promote
17  * products derived from this software without specific prior written
18  * permission.
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  * Any feedback is very welcome.
31  * http://www.math.keio.ac.jp/matumoto/emt.html
32  * email: matumoto@math.keio.ac.jp
33  */
34 
35 /*
36  * #define N 624
37  * #define M 397
38  * #define MATRIX_A 0x9908b0dfUL // constant vector a
39  * #define UPPER_MASK 0x80000000UL // most significant w-r bits
40  * #define LOWER_MASK 0x7fffffffUL // least significant r bits
41  */
42 
43 #define VS_RAND_MAX 0x7fffffffUL
44 
45 class VSRandom
46 {
47 #define NN_CONSTANT 624
48  static const unsigned int N()
49  {
50  return NN_CONSTANT;
51  }
52  static const int M()
53  {
54  return 397;
55  }
56  static const int MATRIX_A()
57  {
58  return 0x9908b0dfUL;
59  }
60  static const int UPPER_MASK()
61  {
62  return 0x80000000UL;
63  }
64  static const int LOWER_MASK()
65  {
66  return 0x7fffffffUL;
67  }
68  unsigned int mt[NN_CONSTANT]; /* the array for the state vector */
69 #undef NN_CONSTANT
70  unsigned int mti; /* mti==N+1 means mt[N] is not initialized */
71 /* initializes mt[N] with a seed */
72 public: VSRandom( unsigned int s ) : mti( N()+1 )
73  {
74  init_genrand( s );
75  }
76  void init_genrand( unsigned int s )
77  {
78  mt[0] = s&0xffffffffUL;
79  for (mti = 1; mti < N(); mti++) {
80  mt[mti] =
81  (1812433253UL*( mt[mti-1]^(mt[mti-1]>>30) )+mti);
82  /*
83  * See Knuth TAOCP Vol2. 3 rd Ed. P.106 for multiplier.
84  * In the previous versions, MSB s of the seed affect
85  * only MSBs of the array mt[].
86  * 2002/01/09 modified by Makoto Matsumoto
87  */
88  mt[mti] &= 0xffffffffUL;
89  /* for >32 bit machines */
90  }
91  }
92 /*
93  * initialize by an array with array-length
94  * init_key is the array for initializing keys
95  * key_length is its length
96  */
97  VSRandom( unsigned int init_key[], unsigned int key_length ) : mti( N()+1 )
98  {
99  unsigned int i, j, k;
100  init_genrand( 19650218UL );
101  i = 1;
102  j = 0;
103  k = (N() > key_length ? N() : key_length);
104  for (; k; k--) {
105  mt[i] = ( mt[i]^( ( mt[i-1]^(mt[i-1]>>30) )*1664525UL ) )
106  +init_key[j]+j; /* non linear */
107  mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
108  i++;
109  j++;
110  if ( i >= N() ) {
111  mt[0] = mt[N()-1];
112  i = 1;
113  }
114  if (j >= key_length) j = 0;
115  }
116  for (k = N()-1; k; k--) {
117  mt[i] = ( mt[i]^( ( mt[i-1]^(mt[i-1]>>30) )*1566083941UL ) )
118  -i; /* non linear */
119  mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
120  i++;
121  if ( i >= N() ) {
122  mt[0] = mt[N()-1];
123  i = 1;
124  }
125  }
126  mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
127  }
128 /* generates a random number on [0,0xffffffff]-interval */
129  unsigned int genrand_int32( void )
130  {
131  unsigned int y;
132  static unsigned int mag01[2] = {0x0UL, MATRIX_A()};
133  /* mag01[x] = x * MATRIX_A for x=0,1 */
134  if ( mti >= N() ) {
135  /* generate N words at one time */
136  unsigned int kk;
137  if (mti == N()+1) /* if init_genrand() has not been called, */
138  init_genrand( 5489UL ); /* a default initial seed is used */
139  for (kk = 0; kk < N()-M(); kk++) {
140  y = ( mt[kk]&UPPER_MASK() )|( mt[kk+1]&LOWER_MASK() );
141  mt[kk] = mt[kk+M()]^(y>>1)^mag01[y&0x1UL];
142  }
143  for (; kk < N()-1; kk++) {
144  y = ( mt[kk]&UPPER_MASK() )|( mt[kk+1]&LOWER_MASK() );
145  mt[kk] = mt[kk+( M()-N() )]^(y>>1)^mag01[y&0x1UL];
146  }
147  y = ( mt[N()-1]&UPPER_MASK() )|( mt[0]&LOWER_MASK() );
148  mt[N()-1] = mt[M()-1]^(y>>1)^mag01[y&0x1UL];
149  mti = 0;
150  }
151  y = mt[mti++];
152  /* Tempering */
153  y ^= (y>>11);
154  y ^= (y<<7)&0x9d2c5680UL;
155  y ^= (y<<15)&0xefc60000UL;
156  y ^= (y>>18);
157  return y;
158  }
159 /* generates a random number on [0,0x7fffffff]-interval */
160  int genrand_int31( void )
161  {
162  return (int) (genrand_int32()>>1);
163  }
164  unsigned int rand()
165  {
166  return genrand_int31();
167  }
168 /* generates a random number on [0,1]-real-interval */
169  double genrand_real1( void )
170  {
171  return genrand_int32()*(1.0/4294967295.0);
172  /* divided by 2^32-1 */
173  }
174 /* generates a random number on [0,1)-real-interval */
175  double genrand_real2( void )
176  {
177  return genrand_int32()*(1.0/4294967296.0);
178  /* divided by 2^32 */
179  }
180  double uniformInc( double min, double max )
181  {
182  return genrand_real1()*(max-min)+min;
183  }
184  double uniformExc( double min, double max )
185  {
186  return genrand_real2()*(max-min)+min;
187  }
188 
189 /* generates a random number on (0,1)-real-interval */
190  double genrand_real3( void )
191  {
192  return ( ( (double) genrand_int32() )+0.5 )*(1.0/4294967296.0);
193  /* divided by 2^32 */
194  }
195 /* generates a random number on [0,1) with 53-bit resolution*/
196  double genrand_res53( void )
197  {
198  unsigned int a = genrand_int32()>>5, b = genrand_int32()>>6;
199  return (a*67108864.0+b)*(1.0/9007199254740992.0);
200  }
201 /* These real versions are due to Isaku Wada, 2002/01/09 added */
202 };
203 extern VSRandom vsrandom;
204