SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Rand.cpp
Go to the documentation of this file.
1 
42 /*
43  * Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
44  *
45  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
46  * license found in the license.txt file in the root directory of this source tree.
47  */
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include "slikenet/Rand.h"
53 
54 //
55 // uint32 must be an unsigned integer type capable of holding at least 32
56 // bits; exactly 32 should be fastest, but 64 is better on an Alpha with
57 // GCC at -O3 optimization so try your options and see what's best for you
58 //
59 
60 //typedef unsigned int uint32;
61 
62 #define N (624) // length of state vector
63 #define M (397) // a period parameter
64 #define K (0x9908B0DFU) // a magic constant
65 #define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u
66 #define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u
67 #define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u
68 #define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v
69 
70 static unsigned int _state[ N + 1 ]; // state vector + 1 extra to not violate ANSI C
71 static unsigned int *_next; // next random value is computed from here
72 static int _left = -1; // can *next++ this many times before reloading
73 
74 using namespace SLNet;
75 
76 void seedMT( unsigned int seed, unsigned int *state, unsigned int *&next, int &left );
77 unsigned int reloadMT( unsigned int *state, unsigned int *&next, int &left );
78 unsigned int randomMT( unsigned int *state, unsigned int *&next, int &left );
79 void fillBufferMT( void *buffer, unsigned int bytes, unsigned int *state, unsigned int *&next, int &left );
80 float frandomMT( unsigned int *state, unsigned int *&next, int &left );
81 
82 // Uses global vars
83 void seedMT( unsigned int seed )
84 {
85  seedMT(seed, _state, _next, _left);
86 }
87 unsigned int reloadMT( void )
88 {
89  return reloadMT(_state, _next, _left);
90 }
91 unsigned int randomMT( void )
92 {
93  return randomMT(_state, _next, _left);
94 }
95 float frandomMT( void )
96 {
97  return frandomMT(_state, _next, _left);
98 }
99 void fillBufferMT( void *buffer, unsigned int bytes )
100 {
101  fillBufferMT(buffer, bytes, _state, _next, _left);
102 }
103 
104 void seedMT( unsigned int seed, unsigned int *state, unsigned int *&next, int &left ) // Defined in cokus_c.c
105 {
106  (void) next;
107 
108  //
109  // We initialize state[0..(N-1)] via the generator
110  //
111  // x_new = (69069 * x_old) mod 2^32
112  //
113  // from Line 15 of Table 1, p. 106, Sec. 3.3.4 of Knuth's
114  // _The Art of Computer Programming_, Volume 2, 3rd ed.
115  //
116  // Notes (SJC): I do not know what the initial state requirements
117  // of the Mersenne Twister are, but it seems this seeding generator
118  // could be better. It achieves the maximum period for its modulus
119  // (2^30) iff x_initial is odd (p. 20-21, Sec. 3.2.1.2, Knuth); if
120  // x_initial can be even, you have sequences like 0, 0, 0, ...;
121  // 2^31, 2^31, 2^31, ...; 2^30, 2^30, 2^30, ...; 2^29, 2^29 + 2^31,
122  // 2^29, 2^29 + 2^31, ..., etc. so I force seed to be odd below.
123  //
124  // Even if x_initial is odd, if x_initial is 1 mod 4 then
125  //
126  // the lowest bit of x is always 1,
127  // the next-to-lowest bit of x is always 0,
128  // the 2nd-from-lowest bit of x alternates ... 0 1 0 1 0 1 0 1 ... ,
129  // the 3rd-from-lowest bit of x 4-cycles ... 0 1 1 0 0 1 1 0 ... ,
130  // the 4th-from-lowest bit of x has the 8-cycle ... 0 0 0 1 1 1 1 0 ... ,
131  // ...
132  //
133  // and if x_initial is 3 mod 4 then
134  //
135  // the lowest bit of x is always 1,
136  // the next-to-lowest bit of x is always 1,
137  // the 2nd-from-lowest bit of x alternates ... 0 1 0 1 0 1 0 1 ... ,
138  // the 3rd-from-lowest bit of x 4-cycles ... 0 0 1 1 0 0 1 1 ... ,
139  // the 4th-from-lowest bit of x has the 8-cycle ... 0 0 1 1 1 1 0 0 ... ,
140  // ...
141  //
142  // The generator's potency (min. s>=0 with (69069-1)^s = 0 mod 2^32) is
143  // 16, which seems to be alright by p. 25, Sec. 3.2.1.3 of Knuth. It
144  // also does well in the dimension 2..5 spectral tests, but it could be
145  // better in dimension 6 (Line 15, Table 1, p. 106, Sec. 3.3.4, Knuth).
146  //
147  // Note that the random number user does not see the values generated
148  // here directly since reloadMT() will always munge them first, so maybe
149  // none of all of this matters. In fact, the seed values made here could
150  // even be extra-special desirable if the Mersenne Twister theory says
151  // so-- that's why the only change I made is to restrict to odd seeds.
152  //
153 
154  unsigned int x = ( seed | 1U ) & 0xFFFFFFFFU, *s = state;
155  int j;
156 
157  for ( left = 0, *s++ = x, j = N; --j;
158  *s++ = ( x *= 69069U ) & 0xFFFFFFFFU )
159 
160  ;
161 }
162 
163 
164 unsigned int reloadMT( unsigned int *state, unsigned int *&next, int &left )
165 {
166  unsigned int * p0 = state, *p2 = state + 2, *pM = state + M, s0, s1;
167  int j;
168 
169  if ( left < -1 )
170  seedMT( 4357U );
171 
172  left = N - 1, next = state + 1;
173 
174  for ( s0 = state[ 0 ], s1 = state[ 1 ], j = N - M + 1; --j; s0 = s1, s1 = *p2++ )
175  * p0++ = *pM++ ^ ( mixBits( s0, s1 ) >> 1 ) ^ ( loBit( s1 ) ? K : 0U );
176 
177  for ( pM = state, j = M; --j; s0 = s1, s1 = *p2++ )
178  * p0++ = *pM++ ^ ( mixBits( s0, s1 ) >> 1 ) ^ ( loBit( s1 ) ? K : 0U );
179 
180  s1 = state[ 0 ], *p0 = *pM ^ ( mixBits( s0, s1 ) >> 1 ) ^ ( loBit( s1 ) ? K : 0U );
181 
182  s1 ^= ( s1 >> 11 );
183 
184  s1 ^= ( s1 << 7 ) & 0x9D2C5680U;
185 
186  s1 ^= ( s1 << 15 ) & 0xEFC60000U;
187 
188  return ( s1 ^ ( s1 >> 18 ) );
189 }
190 
191 
192 unsigned int randomMT( unsigned int *state, unsigned int *&next, int &left )
193 {
194  unsigned int y;
195 
196  if ( --left < 0 )
197  return ( reloadMT(state, next, left) );
198 
199  y = *next++;
200 
201  y ^= ( y >> 11 );
202 
203  y ^= ( y << 7 ) & 0x9D2C5680U;
204 
205  y ^= ( y << 15 ) & 0xEFC60000U;
206 
207  return ( y ^ ( y >> 18 ) );
208 
209  // This change made so the value returned is in the same range as what rand() returns
210  // return(y ^ (y >> 18)) % 32767;
211 }
212 
213 void fillBufferMT( void *buffer, unsigned int bytes, unsigned int *state, unsigned int *&next, int &left )
214 {
215  unsigned int offset=0;
216  unsigned int r;
217  while (bytes-offset>=sizeof(r))
218  {
219  r = randomMT(state, next, left);
220  memcpy((char*)buffer+offset, &r, sizeof(r));
221  offset+=sizeof(r);
222  }
223 
224  r = randomMT(state, next, left);
225  memcpy((char*)buffer+offset, &r, bytes-offset);
226 }
227 
228 float frandomMT( unsigned int *state, unsigned int *&next, int &left )
229 {
230  return ( float ) ( ( double ) randomMT(state, next, left) / 4294967296.0 );
231 }
233 {
234  left=-1;
235 }
237 {
238 }
239 void RakNetRandom::SeedMT( unsigned int seed )
240 {
241  printf("%i\n",seed);
242  seedMT(seed, state, next, left);
243 }
244 
245 unsigned int RakNetRandom::ReloadMT( void )
246 {
247  return reloadMT(state, next, left);
248 }
249 
250 unsigned int RakNetRandom::RandomMT( void )
251 {
252  return randomMT(state, next, left);
253 }
254 
256 {
257  return frandomMT(state, next, left);
258 }
259 
260 void RakNetRandom::FillBufferMT( void *buffer, unsigned int bytes )
261 {
262  fillBufferMT(buffer, bytes, state, next, left);
263 }
264 
265 /*
266 int main(void)
267 {
268 int j;
269 
270 // you can seed with any uint32, but the best are odds in 0..(2^32 - 1)
271 
272 seedMT(4357U);
273 
274 // print the first 2,002 random numbers seven to a line as an example
275 
276 for(j=0; j<2002; j++)
277 RAKNET_DEBUG_PRINTF(" %10lu%s", (unsigned int) randomMT(), (j%7)==6 ? "\n" : "");
278 
279 return(EXIT_SUCCESS);
280 }
281 
282 */
283