]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_random.c
Remove fastmath funktions like discussed on the list.
[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 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.
20 //
21 // $Id$
22
23
24 /* 
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.
29
30    Before using, initialize the state by using init_genrand(seed) 
31    or init_by_array(init_key, key_length).
32
33    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
34    All rights reserved.                          
35
36    Redistribution and use in source and binary forms, with or without
37    modification, are permitted provided that the following conditions
38    are met:
39
40      1. Redistributions of source code must retain the above copyright
41         notice, this list of conditions and the following disclaimer.
42
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.
46
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 
49         permission.
50
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.
62
63
64    Any feedback is very welcome.
65    http://www.math.keio.ac.jp/matumoto/emt.html
66    email: matumoto@math.keio.ac.jp
67 */
68
69 #ifdef HAVE_CONFIG_H
70 #  include <simgear_config.h>
71 #endif
72
73 #include <stdio.h>
74 #include <stdlib.h>         // for random(), srandom()
75 #include <time.h>           // for time() to seed srandom()        
76
77 #include "sg_random.h"
78
79 /* Period parameters */  
80 #define N 624
81 #define M 397
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))
87
88 static unsigned long state[N]; /* the array for the state vector  */
89 static int left = 1;
90 static int initf = 0;
91 static unsigned long *next;
92
93 /* initializes state[N] with a seed */
94 void init_genrand(unsigned long s)
95 {
96     int j;
97     state[0]= s & 0xffffffffUL;
98     for (j=1; j<N; j++) {
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 */
105     }
106     left = 1; initf = 1;
107 }
108
109 static void next_state(void)
110 {
111     unsigned long *p=state;
112     int j;
113
114     /* if init_genrand() has not been called, */
115     /* a default initial seed is used         */
116     if (initf==0) init_genrand(5489UL);
117
118     left = N;
119     next = state;
120
121     for (j=N-M+1; --j; p++) 
122         *p = p[M] ^ TWIST(p[0], p[1]);
123
124     for (j=M; --j; p++) 
125         *p = p[M-N] ^ TWIST(p[0], p[1]);
126
127     *p = p[M-N] ^ TWIST(p[0], state[0]);
128 }
129
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));
134 }
135
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);
141 }
142
143
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 );
148 }
149
150
151 // return a random number between [0.0, 1.0)
152 double sg_random() {
153     unsigned long y;
154
155     if (--left == 0)
156                 next_state();
157     y = *next++;
158
159     /* Tempering */
160     y ^= (y >> 11);
161     y ^= (y << 7) & 0x9d2c5680UL;
162     y ^= (y << 15) & 0xefc60000UL;
163     y ^= (y >> 18);
164
165     return (double)y * (1.0/4294967295.0); 
166     /* divided by 2^32-1 */ 
167 }
168
169