]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_random.c
Add project.* to MSVC project files
[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  * "Cleaned up" and simplified Mersenne Twister implementation.
26  * Vastly smaller and more easily understood and embedded.  Stores the
27  * state in a user-maintained structure instead of static memory, so
28  * you can have more than one, or save snapshots of the RNG state.
29  * Lacks the "init_by_array()" feature of the original code in favor
30  * of the simpler 32 bit seed initialization.  Lacks the floating
31  * point generator, which is an orthogonal problem not related to
32  * random number generation.  Verified to be identical to the original
33  * MT199367ar code through the first 10M generated numbers.
34  */
35
36
37
38 #ifdef HAVE_CONFIG_H
39 #  include <simgear_config.h>
40 #endif
41
42 #include <stdio.h>
43 #include <stdlib.h>         // for random(), srandom()
44 #include <time.h>           // for time() to seed srandom()        
45
46 #include "sg_random.h"
47
48 // Structure for the random number functions.
49 mt random_seed;
50
51 #define MT(i) mt->array[i]
52
53 void mt_init(mt *mt, unsigned int seed)
54 {
55     int i;
56     MT(0)= seed;
57     for(i=1; i<MT_N; i++)
58         MT(i) = (1812433253 * (MT(i-1) ^ (MT(i-1) >> 30)) + i);
59     mt->index = MT_N+1;
60 }
61
62 unsigned int mt_rand32(mt *mt)
63 {
64     unsigned int i, y;
65     if(mt->index >= MT_N) {
66         for(i=0; i<MT_N; i++) {
67             y = (MT(i) & 0x80000000) | (MT((i+1)%MT_N) & 0x7fffffff);
68             MT(i) = MT((i+MT_M)%MT_N) ^ (y>>1) ^ (y&1 ? 0x9908b0df : 0);
69         }
70         mt->index = 0;
71     }
72     y = MT(mt->index++);
73     y ^= (y >> 11);
74     y ^= (y << 7) & 0x9d2c5680;
75     y ^= (y << 15) & 0xefc60000;
76     y ^= (y >> 18);
77     return y;
78 }
79
80 double mt_rand(mt *mt)
81 {
82     /* divided by 2^32-1 */ 
83     return (double)mt_rand32(mt) * (1.0/4294967295.0); 
84 }
85
86 // Seed the random number generater with time() so we don't see the
87 // same sequence every time
88 void sg_srandom_time() {
89     mt_init(&random_seed, (unsigned int) time(NULL));
90 }
91
92 // Seed the random number generater with time() in 10 minute intervals
93 // so we get the same sequence within 10 minutes interval.
94 // This is useful for synchronizing two display systems.
95 void sg_srandom_time_10() {
96     mt_init(&random_seed, (unsigned int) time(NULL) / 600);
97 }
98
99 // Seed the random number generater with your own seed so can set up
100 // repeatable randomization.
101 void sg_srandom( unsigned int seed ) {
102     mt_init(&random_seed, seed);
103 }
104
105 // return a random number between [0.0, 1.0)
106 double sg_random() {
107   return mt_rand(&random_seed);
108 }
109