1 /* Note about copyright: This is now available for free commercial use
2 according to their web site. We just need to provide our changed version
3 in the public domain, and note changes we make in this file. -- Bill Cox
4
5 July 20, 2003 - Changed interface and formating to just provide utSetSeed
6 and utRand, and modified types to conform to DataDraw types -- Bill Cox
7 */
8
9 /* A C-program for MT19937: Integer version (1999/10/28) */
10 /* genrand() generates one pseudorandom unsigned integer (32bit) */
11 /* which is uniformly distributed among 0 to 2^32-1 for each */
12 /* call. sgenrand(seed) sets initial values to the working area */
13 /* of 624 words. Before genrand(), sgenrand(seed) must be */
14 /* called once. (seed is any 32-bit integer.) */
15 /* Coded by Takuji Nishimura, considering the suggestions by */
16 /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */
17
18 /* This library is free software under the Artistic license: */
19 /* see the file COPYING distributed together with this code. */
20 /* For the verification of the code, its output sequence file */
21 /* mt19937int.out is attached (2001/4/2) */
22
23 /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */
24 /* Any feedback is very welcome. For any question, comments, */
25 /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */
26 /* matumoto@math.keio.ac.jp */
27
28 /* REFERENCE */
29 /* M. Matsumoto and T. Nishimura, */
30 /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */
31 /* Pseudo-Random Number Generator", */
32 /* ACM Transactions on Modeling and Computer Simulation, */
33 /* Vol. 8, No. 1, January 1998, pp 3--30. */
34
35 #include "uttypes.h"
36
37 /* Period parameters */
38 #define N 624
39 #define M 397
40 #define MATRIX_A 0x9908b0df /* constant vector a */
41 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
42 #define LOWER_MASK 0x7fffffff /* least significant r bits */
43
44 /* Tempering parameters */
45 #define TEMPERING_MASK_B 0x9d2c5680
46 #define TEMPERING_MASK_C 0xefc60000
47 #define TEMPERING_SHIFT_U(y) (y >> 11)
48 #define TEMPERING_SHIFT_S(y) (y << 7)
49 #define TEMPERING_SHIFT_T(y) (y << 15)
50 #define TEMPERING_SHIFT_L(y) (y >> 18)
51
52 static uint32 mt[N]; /* the array for the state vector */
53 static int32 mti;
54
55 /* Initializing the array with a seed */
utInitSeed(uint32 seed)56 void utInitSeed(
57 uint32 seed)
58 {
59 int32 i;
60
61 for (i=0;i<N;i++) {
62 mt[i] = seed & 0xffff0000;
63 seed = 69069 * seed + 1;
64 mt[i] |= (seed & 0xffff0000) >> 16;
65 seed = 69069 * seed + 1;
66 }
67 mti = N;
68 }
69
utRand(void)70 uint32 utRand(void)
71 {
72 uint32 y;
73 int32 kk;
74 static uint32 mag01[2]={0x0, MATRIX_A};
75 /* mag01[x] = x * MATRIX_A for x=0,1 */
76
77 if (mti >= N) { /* generate N words at one time */
78 for (kk=0;kk<N-M;kk++) {
79 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
80 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
81 }
82 for (;kk<N-1;kk++) {
83 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
84 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
85 }
86 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
87 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
88 mti = 0;
89 }
90 y = mt[mti++];
91 y ^= TEMPERING_SHIFT_U(y);
92 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
93 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
94 y ^= TEMPERING_SHIFT_L(y);
95 return y;
96 }
97