1 // SPDX-License-Identifier: GPL-2.0+
3 * random.c -- pseudo random number generator
4 * Copyright (C) 1994 Chris Wallace (csw@bruce.cs.monash.edu.au)
10 * modified by dxm@sgi.com so that this file acts as a drop in replacement
11 * for srandom and random.
15 * A random number generator called as a function by
16 * random (iseed) or irandm (iseed)
17 * The parameter should be a pointer to a 2-element int32_t vector.
18 * The first function returns a double uniform in 0 .. 1.
19 * The second returns a int32_t integer uniform in 0 .. 2**31-1
20 * Both update iseed[] in exactly the same way.
21 * iseed[] must be a 2-element integer vector.
22 * The initial value of the second element may be anything.
24 * The period of the random sequence is 2**32 * (2**32-1)
25 * The table mt[0:127] is defined by mt[i] = 69069 ** (128-i)
28 #define MASK ((int32_t) 593970775)
29 /* or in hex, 23674657 */
31 #define SCALE ((double) 1.0 / (1024.0 * 1024.0 * 1024.0 * 2.0))
32 /* i.e. 2 to power -31 */
34 static int32_t mt [128] = {
166 _random (int32_t is [2])
168 int32_t it, leh, nit;
173 it = (it + it) ^ MASK;
177 /* to ensure all-ones pattern omitted */
178 leh = leh * mt[nit & 127] + nit;
179 is [0] = it; is [1] = leh;
180 if (leh < 0) leh = ~leh;
181 return (SCALE * ((int32_t) (leh | 1)));
187 _irandm (int32_t is [2])
189 int32_t it, leh, nit;
194 it = (it + it) ^ MASK;
198 /* to ensure all-ones pattern omitted */
199 leh = leh * mt[nit & 127] + nit;
200 is [0] = it; is [1] = leh;
201 if (leh < 0) leh = ~leh;
206 * make this a drop in replacement for random and srandom
208 * XXX not thread safe I guess.
211 static int32_t saved_seed[2];
215 return _irandm(saved_seed);
218 void srandom(unsigned seed)