head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.40; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.57; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.04; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.00; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# sg_random.c

    00001 // sg_random.c -- routines to handle random number generation
    00002 //
    00003 // Written by Curtis Olson, started July 1997.
    00004 //
    00005 // Copyright (C) 1997  Curtis L. Olson  - curt@@infoplane.com
    00006 //
    00007 // This library is free software; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Library General Public
    00009 // License as published by the Free Software Foundation; either
    00010 // version 2 of the License, or (at your option) any later version.
    00011 //
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015 // Library General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Library General Public
    00018 // License along with this library; if not, write to the
    00019 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00020 // Boston, MA  02111-1307, USA.
    00021 //
    00022 // $Id: sg_random.c,v 1.7 2002/08/05 18:43:28 david Exp $
    00023
    00024
    00025 /*
    00026    A C-program for MT19937, with initialization improved 2002/2/10.
    00027    Coded by Takuji Nishimura and Makoto Matsumoto.
    00028    This is a faster version by taking Shawn Cokus's optimization,
    00029    Matthe Bellew's simplification, Isaku Wada's real version.
    00030
    00031    Before using, initialize the state by using init_genrand(seed)
    00032    or init_by_array(init_key, key_length).
    00033
    00034    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
    00035    All rights reserved.
    00036
    00037    Redistribution and use in source and binary forms, with or without
    00038    modification, are permitted provided that the following conditions
    00039    are met:
    00040
    00041      1. Redistributions of source code must retain the above copyright
    00042         notice, this list of conditions and the following disclaimer.
    00043
    00044      2. Redistributions in binary form must reproduce the above copyright
    00045         notice, this list of conditions and the following disclaimer in the
    00046         documentation and/or other materials provided with the distribution.
    00047
    00048      3. The names of its contributors may not be used to endorse or promote
    00049         products derived from this software without specific prior written
    00050         permission.
    00051
    00052    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    00053    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    00054    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    00055    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    00056    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    00057    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    00058    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    00059    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    00060    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00061    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00062    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00063
    00064
    00065    Any feedback is very welcome.
    00066    http://www.math.keio.ac.jp/matumoto/emt.html
    00067    email: matumoto@@math.keio.ac.jp
    00068 */
    00069
    00070 #ifdef HAVE_CONFIG_H
    00071 #  include <simgear_config.h>
    00072 #endif
    00073
    00074 #include <stdio.h>
    00075 #include <stdlib.h>         // for random(), srandom()
    00076 #include <time.h>           // for time() to seed srandom()
    00077
    00078 #include "sg_random.h"
    00079
    00080 /* Period parameters */
    00081 #define N 624
    00082 #define M 397
    00083 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
    00084 #define UMASK 0x80000000UL /* most significant w-r bits */
    00085 #define LMASK 0x7fffffffUL /* least significant r bits */
    00086 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
    00087 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
    00088
    00089 static unsigned long state[N]; /* the array for the state vector  */
    00090 static int left = 1;
    00091 static int initf = 0;
    00092 static unsigned long *next;
    00093
    00094 /* initializes state[N] with a seed */
    00095 void init_genrand(unsigned long s)
    00096 {
    00097     int j;
    00098     state[0]= s & 0xffffffffUL;
    00099     for (j=1; j<N; j++) {
    00100         state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
    00101         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
    00102         /* In the previous versions, MSBs of the seed affect   */
    00103         /* only MSBs of the array state[].                        */
    00104         /* 2002/01/09 modified by Makoto Matsumoto             */
    00105         state[j] &= 0xffffffffUL;  /* for >32 bit machines */
    00106     }
    00107     left = 1; initf = 1;
    00108 }
    00109
    00110 static void next_state(void)
    00111 {
    00112     unsigned long *p=state;
    00113     int j;
    00114
    00115     /* if init_genrand() has not been called, */
    00116     /* a default initial seed is used         */
    00117     if (initf==0) init_genrand(5489UL);
    00118
    00119     left = N;
    00120     next = state;
    00121
    00122     for (j=N-M+1; --j; p++)
    00123         *p = p[M] ^ TWIST(p[0], p[1]);
    00124
    00125     for (j=M; --j; p++)
    00126         *p = p[M-N] ^ TWIST(p[0], p[1]);
    00127
    00128     *p = p[M-N] ^ TWIST(p[0], state[0]);
    00129 }
    00130
    00131 // Seed the random number generater with time() so we don't see the
    00132 // same sequence every time
    00133 void sg_srandom_time() {
    00134     init_genrand(time(NULL));
    00135 }
    00136
    00137
    00138 // Seed the random number generater with your own seed so can set up
    00139 // repeatable randomization.
    00140 void sg_srandom( unsigned int seed ) {
    00141     init_genrand( seed );
    00142 }
    00143
    00144
    00145 // return a random number between [0.0, 1.0)
    00146 double sg_random() {
    00147     unsigned long y;
    00148
    00149     if (--left == 0)
    00150                 next_state();
    00151     y = *next++;
    00152
    00153     /* Tempering */
    00154     y ^= (y >> 11);
    00155     y ^= (y << 7) & 0x9d2c5680UL;
    00156     y ^= (y << 15) & 0xefc60000UL;
    00157     y ^= (y >> 18);
    00158
    00159     return (double)y * (1.0/4294967295.0);
    00160     /* divided by 2^32-1 */
    00161 }
    00162
    00163

@ 1.3 log @Oops, try again. @ text @d172 1 a172 1