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 Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA 02111-1307, USA.
26 A C-program for MT19937, with initialization improved 2002/2/10.
27 Coded by Takuji Nishimura and Makoto Matsumoto.
28 This is a faster version by taking Shawn Cokus's optimization,
29 Matthe Bellew's simplification, Isaku Wada's real version.
31 Before using, initialize the state by using init_genrand(seed)
32 or init_by_array(init_key, key_length).
34 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
37 Redistribution and use in source and binary forms, with or without
38 modification, are permitted provided that the following conditions
41 1. Redistributions of source code must retain the above copyright
42 notice, this list of conditions and the following disclaimer.
44 2. Redistributions in binary form must reproduce the above copyright
45 notice, this list of conditions and the following disclaimer in the
46 documentation and/or other materials provided with the distribution.
48 3. The names of its contributors may not be used to endorse or promote
49 products derived from this software without specific prior written
52 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
56 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
57 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
58 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
59 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
60 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
61 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 Any feedback is very welcome.
66 http://www.math.keio.ac.jp/matumoto/emt.html
67 email: matumoto@math.keio.ac.jp
71 # include <simgear_config.h>
75 #include <stdlib.h> // for random(), srandom()
76 #include <time.h> // for time() to seed srandom()
78 #include "sg_random.h"
80 /* Period parameters */
83 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
84 #define UMASK 0x80000000UL /* most significant w-r bits */
85 #define LMASK 0x7fffffffUL /* least significant r bits */
86 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
87 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
89 static unsigned long state[N]; /* the array for the state vector */
92 static unsigned long *next;
94 /* initializes state[N] with a seed */
95 void init_genrand(unsigned long s)
98 state[0]= s & 0xffffffffUL;
100 state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
101 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
102 /* In the previous versions, MSBs of the seed affect */
103 /* only MSBs of the array state[]. */
104 /* 2002/01/09 modified by Makoto Matsumoto */
105 state[j] &= 0xffffffffUL; /* for >32 bit machines */
110 static void next_state(void)
112 unsigned long *p=state;
115 /* if init_genrand() has not been called, */
116 /* a default initial seed is used */
117 if (initf==0) init_genrand(5489UL);
122 for (j=N-M+1; --j; p++)
123 *p = p[M] ^ TWIST(p[0], p[1]);
126 *p = p[M-N] ^ TWIST(p[0], p[1]);
128 *p = p[M-N] ^ TWIST(p[0], state[0]);
131 // Seed the random number generater with time() so we don't see the
132 // same sequence every time
133 void sg_srandom_time() {
134 init_genrand(time(NULL));
137 // Seed the random number generater with time() in 10 minute intervals
138 // so we get the same sequence within 10 minutes interval.
139 // This is useful for synchronizing two display systems.
140 void sg_srandom_time_10() {
141 init_genrand(time(NULL) / 600);
145 // Seed the random number generater with your own seed so can set up
146 // repeatable randomization.
147 void sg_srandom( unsigned int seed ) {
148 init_genrand( seed );
152 // return a random number between [0.0, 1.0)
162 y ^= (y << 7) & 0x9d2c5680UL;
163 y ^= (y << 15) & 0xefc60000UL;
166 return (double)y * (1.0/4294967295.0);
167 /* divided by 2^32-1 */