1 /**************************************************************************
3 * random.c -- pseudo random number generator
4 * Copyright (C) 1994 Chris Wallace (csw@bruce.cs.monash.edu.au)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 **************************************************************************/
25 * modified by dxm@sgi.com so that this file acts as a drop in replacement
26 * for srandom and random.
30 * A random number generator called as a function by
31 * random (iseed) or irandm (iseed)
32 * The parameter should be a pointer to a 2-element long vector.
33 * The first function returns a double uniform in 0 .. 1.
34 * The second returns a long integer uniform in 0 .. 2**31-1
35 * Both update iseed[] in exactly the same way.
36 * iseed[] must be a 2-element integer vector.
37 * The initial value of the second element may be anything.
39 * The period of the random sequence is 2**32 * (2**32-1)
40 * The table mt[0:127] is defined by mt[i] = 69069 ** (128-i)
43 #define MASK ((long) 593970775)
44 /* or in hex, 23674657 */
46 #define SCALE ((double) 1.0 / (1024.0 * 1024.0 * 1024.0 * 2.0))
47 /* i.e. 2 to power -31 */
49 static long mt [128] = {
181 _random (long is [2])
188 it = (it + it) ^ MASK;
192 /* to ensure all-ones pattern omitted */
193 leh = leh * mt[nit & 127] + nit;
194 is [0] = it; is [1] = leh;
195 if (leh < 0) leh = ~leh;
196 return (SCALE * ((long) (leh | 1)));
202 _irandm (long is [2])
209 it = (it + it) ^ MASK;
213 /* to ensure all-ones pattern omitted */
214 leh = leh * mt[nit & 127] + nit;
215 is [0] = it; is [1] = leh;
216 if (leh < 0) leh = ~leh;
221 * make this a drop in replacement for random and srandom
223 * XXX not thread safe I guess.
226 static long saved_seed[2];
230 return _irandm(saved_seed);
233 void srandom(unsigned seed)