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 * "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.
39 # include <simgear_config.h>
43 #include <stdlib.h> // for random(), srandom()
44 #include <time.h> // for time() to seed srandom()
46 #include "sg_random.h"
48 // Structure for the random number functions.
51 #define MT(i) mt->array[i]
53 void mt_init(mt *mt, unsigned int seed)
58 MT(i) = (1812433253 * (MT(i-1) ^ (MT(i-1) >> 30)) + i);
62 unsigned int mt_rand32(mt *mt)
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);
74 y ^= (y << 7) & 0x9d2c5680;
75 y ^= (y << 15) & 0xefc60000;
80 double mt_rand(mt *mt)
82 /* divided by 2^32-1 */
83 return (double)mt_rand32(mt) * (1.0/4294967295.0);
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));
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);
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);
105 // return a random number between [0.0, 1.0)
107 return mt_rand(&random_seed);