1 // sg_random.c -- routines to handle random number generation
3 // Written by Curtis Olson, started July 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 A C-program for MT19937, with initialization improved 2002/2/10.
26 Coded by Takuji Nishimura and Makoto Matsumoto.
27 This is a faster version by taking Shawn Cokus's optimization,
28 Matthe Bellew's simplification, Isaku Wada's real version.
30 Before using, initialize the state by using init_genrand(seed)
31 or init_by_array(init_key, key_length).
33 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
36 Redistribution and use in source and binary forms, with or without
37 modification, are permitted provided that the following conditions
40 1. Redistributions of source code must retain the above copyright
41 notice, this list of conditions and the following disclaimer.
43 2. Redistributions in binary form must reproduce the above copyright
44 notice, this list of conditions and the following disclaimer in the
45 documentation and/or other materials provided with the distribution.
47 3. The names of its contributors may not be used to endorse or promote
48 products derived from this software without specific prior written
51 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
55 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
58 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
59 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
60 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 Any feedback is very welcome.
65 http://www.math.keio.ac.jp/matumoto/emt.html
66 email: matumoto@math.keio.ac.jp
70 # include <simgear_config.h>
74 #include <stdlib.h> // for random(), srandom()
75 #include <time.h> // for time() to seed srandom()
77 #include "sg_random.h"
79 /* Period parameters */
82 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
83 #define UMASK 0x80000000UL /* most significant w-r bits */
84 #define LMASK 0x7fffffffUL /* least significant r bits */
85 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
86 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
88 static unsigned long state[N]; /* the array for the state vector */
91 static unsigned long *next;
93 /* initializes state[N] with a seed */
94 void init_genrand(unsigned long s)
97 state[0]= s & 0xffffffffUL;
99 state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
100 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
101 /* In the previous versions, MSBs of the seed affect */
102 /* only MSBs of the array state[]. */
103 /* 2002/01/09 modified by Makoto Matsumoto */
104 state[j] &= 0xffffffffUL; /* for >32 bit machines */
109 static void next_state(void)
111 unsigned long *p=state;
114 /* if init_genrand() has not been called, */
115 /* a default initial seed is used */
116 if (initf==0) init_genrand(5489UL);
121 for (j=N-M+1; --j; p++)
122 *p = p[M] ^ TWIST(p[0], p[1]);
125 *p = p[M-N] ^ TWIST(p[0], p[1]);
127 *p = p[M-N] ^ TWIST(p[0], state[0]);
130 // Seed the random number generater with time() so we don't see the
131 // same sequence every time
132 void sg_srandom_time() {
133 init_genrand(time(NULL));
136 // Seed the random number generater with time() in 10 minute intervals
137 // so we get the same sequence within 10 minutes interval.
138 // This is useful for synchronizing two display systems.
139 void sg_srandom_time_10() {
140 init_genrand(time(NULL) / 600);
144 // Seed the random number generater with your own seed so can set up
145 // repeatable randomization.
146 void sg_srandom( unsigned int seed ) {
147 init_genrand( seed );
151 // return a random number between [0.0, 1.0)
161 y ^= (y << 7) & 0x9d2c5680UL;
162 y ^= (y << 15) & 0xefc60000UL;
165 return (double)y * (1.0/4294967295.0);
166 /* divided by 2^32-1 */