]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_random.c
Update a few more instances of my email address.
[simgear.git] / simgear / math / sg_random.c
1 // sg_random.c -- routines to handle random number generation
2 //
3 // Written by Curtis Olson, started July 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
21 //
22 // $Id$
23
24
25 /* 
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.
30
31    Before using, initialize the state by using init_genrand(seed) 
32    or init_by_array(init_key, key_length).
33
34    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
35    All rights reserved.                          
36
37    Redistribution and use in source and binary forms, with or without
38    modification, are permitted provided that the following conditions
39    are met:
40
41      1. Redistributions of source code must retain the above copyright
42         notice, this list of conditions and the following disclaimer.
43
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.
47
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 
50         permission.
51
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.
63
64
65    Any feedback is very welcome.
66    http://www.math.keio.ac.jp/matumoto/emt.html
67    email: matumoto@math.keio.ac.jp
68 */
69
70 #ifdef HAVE_CONFIG_H
71 #  include <simgear_config.h>
72 #endif
73
74 #include <stdio.h>
75 #include <stdlib.h>         // for random(), srandom()
76 #include <time.h>           // for time() to seed srandom()        
77
78 #include "sg_random.h"
79
80 /* Period parameters */  
81 #define N 624
82 #define M 397
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))
88
89 static unsigned long state[N]; /* the array for the state vector  */
90 static int left = 1;
91 static int initf = 0;
92 static unsigned long *next;
93
94 /* initializes state[N] with a seed */
95 void init_genrand(unsigned long s)
96 {
97     int j;
98     state[0]= s & 0xffffffffUL;
99     for (j=1; j<N; j++) {
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 */
106     }
107     left = 1; initf = 1;
108 }
109
110 static void next_state(void)
111 {
112     unsigned long *p=state;
113     int j;
114
115     /* if init_genrand() has not been called, */
116     /* a default initial seed is used         */
117     if (initf==0) init_genrand(5489UL);
118
119     left = N;
120     next = state;
121
122     for (j=N-M+1; --j; p++) 
123         *p = p[M] ^ TWIST(p[0], p[1]);
124
125     for (j=M; --j; p++) 
126         *p = p[M-N] ^ TWIST(p[0], p[1]);
127
128     *p = p[M-N] ^ TWIST(p[0], state[0]);
129 }
130
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));
135 }
136
137
138 // Seed the random number generater with your own seed so can set up
139 // repeatable randomization.
140 void sg_srandom( unsigned int seed ) {
141     init_genrand( seed );
142 }
143
144
145 // return a random number between [0.0, 1.0)
146 double sg_random() {
147     unsigned long y;
148
149     if (--left == 0)
150                 next_state();
151     y = *next++;
152
153     /* Tempering */
154     y ^= (y >> 11);
155     y ^= (y << 7) & 0x9d2c5680UL;
156     y ^= (y << 15) & 0xefc60000UL;
157     y ^= (y >> 18);
158
159     return (double)y * (1.0/4294967295.0); 
160     /* divided by 2^32-1 */ 
161 }
162
163