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 **************************************************************************/
23 #include <sys/types.h>
26 * modified by dxm@sgi.com so that this file acts as a drop in replacement
27 * for srandom and random.
31 * A random number generator called as a function by
32 * random (iseed) or irandm (iseed)
33 * The parameter should be a pointer to a 2-element int32_t vector.
34 * The first function returns a double uniform in 0 .. 1.
35 * The second returns a int32_t integer uniform in 0 .. 2**31-1
36 * Both update iseed[] in exactly the same way.
37 * iseed[] must be a 2-element integer vector.
38 * The initial value of the second element may be anything.
40 * The period of the random sequence is 2**32 * (2**32-1)
41 * The table mt[0:127] is defined by mt[i] = 69069 ** (128-i)
44 #define MASK ((int32_t) 593970775)
45 /* or in hex, 23674657 */
47 #define SCALE ((double) 1.0 / (1024.0 * 1024.0 * 1024.0 * 2.0))
48 /* i.e. 2 to power -31 */
50 static int32_t mt [128] = {
182 _random (int32_t is [2])
184 int32_t it, leh, nit;
189 it = (it + it) ^ MASK;
193 /* to ensure all-ones pattern omitted */
194 leh = leh * mt[nit & 127] + nit;
195 is [0] = it; is [1] = leh;
196 if (leh < 0) leh = ~leh;
197 return (SCALE * ((int32_t) (leh | 1)));
203 _irandm (int32_t is [2])
205 int32_t it, leh, nit;
210 it = (it + it) ^ MASK;
214 /* to ensure all-ones pattern omitted */
215 leh = leh * mt[nit & 127] + nit;
216 is [0] = it; is [1] = leh;
217 if (leh < 0) leh = ~leh;
222 * make this a drop in replacement for random and srandom
224 * XXX not thread safe I guess.
227 static int32_t saved_seed[2];
231 return _irandm(saved_seed);
234 void srandom(unsigned seed)