| 71 |
theseven |
1 |
/*
|
|
|
2 |
A C-program for MT19937, with initialization improved 2002/2/10.
|
|
|
3 |
Coded by Takuji Nishimura and Makoto Matsumoto.
|
|
|
4 |
This is a faster version by taking Shawn Cokus's optimization,
|
|
|
5 |
Matthe Bellew's simplification.
|
|
|
6 |
|
|
|
7 |
Before using, initialize the state by using srand(seed).
|
|
|
8 |
|
|
|
9 |
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
|
|
10 |
All rights reserved.
|
|
|
11 |
|
|
|
12 |
Redistribution and use in source and binary forms, with or without
|
|
|
13 |
modification, are permitted provided that the following conditions
|
|
|
14 |
are met:
|
|
|
15 |
|
|
|
16 |
1. Redistributions of source code must retain the above copyright
|
|
|
17 |
notice, this list of conditions and the following disclaimer.
|
|
|
18 |
|
|
|
19 |
2. Redistributions in binary form must reproduce the above copyright
|
|
|
20 |
notice, this list of conditions and the following disclaimer in the
|
|
|
21 |
documentation and/or other materials provided with the distribution.
|
|
|
22 |
|
|
|
23 |
3. The names of its contributors may not be used to endorse or promote
|
|
|
24 |
products derived from this software without specific prior written
|
|
|
25 |
permission.
|
|
|
26 |
|
|
|
27 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
28 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
29 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
30 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
31 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
32 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
33 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
34 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
35 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
36 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
37 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
38 |
|
|
|
39 |
Any feedback is very welcome.
|
|
|
40 |
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
|
|
41 |
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
|
|
|
42 |
*/
|
|
|
43 |
|
|
|
44 |
/*
|
|
|
45 |
Adapted to Rockbox by Jens Arnold
|
|
|
46 |
*/
|
|
|
47 |
|
|
|
48 |
#include "global.h"
|
| 113 |
theseven |
49 |
#include "include/stdlib.h"
|
| 71 |
theseven |
50 |
|
|
|
51 |
/* Period parameters */
|
|
|
52 |
#define N 624
|
|
|
53 |
#define M 397
|
|
|
54 |
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
|
|
|
55 |
#define UMASK 0x80000000UL /* most significant w-r bits */
|
|
|
56 |
#define LMASK 0x7fffffffUL /* least significant r bits */
|
|
|
57 |
#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
|
|
|
58 |
#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
|
|
|
59 |
|
|
|
60 |
static unsigned long state[N]; /* the array for the state vector */
|
|
|
61 |
static int left = 0;
|
|
|
62 |
static unsigned long *next;
|
|
|
63 |
|
|
|
64 |
/* initializes state[N] with a seed */
|
|
|
65 |
void srand(unsigned int seed)
|
|
|
66 |
{
|
|
|
67 |
unsigned long x = seed & 0xffffffffUL;
|
|
|
68 |
unsigned long *s = state;
|
|
|
69 |
int j;
|
|
|
70 |
|
|
|
71 |
for (*s++ = x, j = 1; j < N; j++) {
|
|
|
72 |
x = (1812433253UL * (x ^ (x >> 30)) + j) & 0xffffffffUL;
|
|
|
73 |
*s++ = x;
|
|
|
74 |
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
|
|
75 |
/* In the previous versions, MSBs of the seed affect */
|
|
|
76 |
/* only MSBs of the array state[]. */
|
|
|
77 |
/* 2002/01/09 modified by Makoto Matsumoto */
|
|
|
78 |
}
|
|
|
79 |
left = 1;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
static void next_state(void)
|
|
|
83 |
{
|
|
|
84 |
unsigned long *p = state;
|
|
|
85 |
int j;
|
|
|
86 |
|
|
|
87 |
/* if srand() has not been called, */
|
|
|
88 |
/* a default initial seed is used */
|
|
|
89 |
if (left < 0)
|
|
|
90 |
srand(5489UL);
|
|
|
91 |
|
|
|
92 |
left = N;
|
|
|
93 |
next = state;
|
|
|
94 |
|
|
|
95 |
for (j = N - M + 1; --j; p++)
|
|
|
96 |
*p = p[M] ^ TWIST(p[0], p[1]);
|
|
|
97 |
|
|
|
98 |
for (j = M; --j; p++)
|
|
|
99 |
*p = p[M-N] ^ TWIST(p[0], p[1]);
|
|
|
100 |
|
|
|
101 |
*p = p[M-N] ^ TWIST(p[0], state[0]);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/* generates a random number on [0,RAND_MAX]-interval */
|
|
|
105 |
int rand(void)
|
|
|
106 |
{
|
|
|
107 |
unsigned long y;
|
|
|
108 |
|
|
|
109 |
if (--left <= 0)
|
|
|
110 |
next_state();
|
|
|
111 |
y = *next++;
|
|
|
112 |
|
|
|
113 |
/* Tempering */
|
|
|
114 |
y ^= (y >> 11);
|
|
|
115 |
y ^= (y << 7) & 0x9d2c5680UL;
|
|
|
116 |
y ^= (y << 15) & 0xefc60000UL;
|
|
|
117 |
y ^= (y >> 18);
|
|
|
118 |
|
|
|
119 |
return ((unsigned int)y) >> 1;
|
|
|
120 |
}
|