xref: /openbsd-src/lib/libc/stdlib/random.c (revision e34cb67ca6a03d6ed8f381b6dd30bd8428efd463)
1*e34cb67cStb /*	$OpenBSD: random.c,v 1.31 2017/11/28 06:55:49 tb Exp $ */
2df930be7Sderaadt /*
3df930be7Sderaadt  * Copyright (c) 1983 Regents of the University of California.
4df930be7Sderaadt  * All rights reserved.
5df930be7Sderaadt  *
6df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
7df930be7Sderaadt  * modification, are permitted provided that the following conditions
8df930be7Sderaadt  * are met:
9df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
10df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
11df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
13df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
146580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
15df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
16df930be7Sderaadt  *    without specific prior written permission.
17df930be7Sderaadt  *
18df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df930be7Sderaadt  * SUCH DAMAGE.
29df930be7Sderaadt  */
30df930be7Sderaadt 
319b26c4b3Smillert #include <fcntl.h>
32df930be7Sderaadt #include <stdio.h>
33df930be7Sderaadt #include <stdlib.h>
349b26c4b3Smillert #include <unistd.h>
35df930be7Sderaadt 
36bb2902e7Stedu #include "thread_private.h"
37bb2902e7Stedu 
38df930be7Sderaadt /*
39df930be7Sderaadt  * random.c:
40df930be7Sderaadt  *
41df930be7Sderaadt  * An improved random number generation package.  In addition to the standard
42df930be7Sderaadt  * rand()/srand() like interface, this package also has a special state info
43df930be7Sderaadt  * interface.  The initstate() routine is called with a seed, an array of
44df930be7Sderaadt  * bytes, and a count of how many bytes are being passed in; this array is
45df930be7Sderaadt  * then initialized to contain information for random number generation with
46df930be7Sderaadt  * that much state information.  Good sizes for the amount of state
47df930be7Sderaadt  * information are 32, 64, 128, and 256 bytes.  The state can be switched by
48df930be7Sderaadt  * calling the setstate() routine with the same array as was initiallized
49df930be7Sderaadt  * with initstate().  By default, the package runs with 128 bytes of state
50df930be7Sderaadt  * information and generates far better random numbers than a linear
51df930be7Sderaadt  * congruential generator.  If the amount of state information is less than
52df930be7Sderaadt  * 32 bytes, a simple linear congruential R.N.G. is used.
53df930be7Sderaadt  *
5417ba7080Smillert  * Internally, the state information is treated as an array of int32_t; the
55df930be7Sderaadt  * zeroeth element of the array is the type of R.N.G. being used (small
56df930be7Sderaadt  * integer); the remainder of the array is the state information for the
5717ba7080Smillert  * R.N.G.  Thus, 32 bytes of state information will give 7 int32_ts worth of
58df930be7Sderaadt  * state information, which will allow a degree seven polynomial.  (Note:
59df930be7Sderaadt  * the zeroeth word of state information also has some other information
60df930be7Sderaadt  * stored in it -- see setstate() for details).
61df930be7Sderaadt  *
62df930be7Sderaadt  * The random number generation technique is a linear feedback shift register
63df930be7Sderaadt  * approach, employing trinomials (since there are fewer terms to sum up that
64df930be7Sderaadt  * way).  In this approach, the least significant bit of all the numbers in
65df930be7Sderaadt  * the state table will act as a linear feedback shift register, and will
66df930be7Sderaadt  * have period 2^deg - 1 (where deg is the degree of the polynomial being
67df930be7Sderaadt  * used, assuming that the polynomial is irreducible and primitive).  The
68df930be7Sderaadt  * higher order bits will have longer periods, since their values are also
69df930be7Sderaadt  * influenced by pseudo-random carries out of the lower bits.  The total
70df930be7Sderaadt  * period of the generator is approximately deg*(2**deg - 1); thus doubling
71df930be7Sderaadt  * the amount of state information has a vast influence on the period of the
72df930be7Sderaadt  * generator.  Note: the deg*(2**deg - 1) is an approximation only good for
73df930be7Sderaadt  * large deg, when the period of the shift register is the dominant factor.
74df930be7Sderaadt  * With deg equal to seven, the period is actually much longer than the
75df930be7Sderaadt  * 7*(2**7 - 1) predicted by this formula.
76df930be7Sderaadt  */
77df930be7Sderaadt 
78df930be7Sderaadt /*
79df930be7Sderaadt  * For each of the currently supported random number generators, we have a
80df930be7Sderaadt  * break value on the amount of state information (you need at least this
81df930be7Sderaadt  * many bytes of state info to support this random number generator), a degree
82df930be7Sderaadt  * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
83df930be7Sderaadt  * the separation between the two lower order coefficients of the trinomial.
84df930be7Sderaadt  */
85df930be7Sderaadt #define	TYPE_0		0		/* linear congruential */
86df930be7Sderaadt #define	BREAK_0		8
87df930be7Sderaadt #define	DEG_0		0
88df930be7Sderaadt #define	SEP_0		0
89df930be7Sderaadt 
90df930be7Sderaadt #define	TYPE_1		1		/* x**7 + x**3 + 1 */
91df930be7Sderaadt #define	BREAK_1		32
92df930be7Sderaadt #define	DEG_1		7
93df930be7Sderaadt #define	SEP_1		3
94df930be7Sderaadt 
95df930be7Sderaadt #define	TYPE_2		2		/* x**15 + x + 1 */
96df930be7Sderaadt #define	BREAK_2		64
97df930be7Sderaadt #define	DEG_2		15
98df930be7Sderaadt #define	SEP_2		1
99df930be7Sderaadt 
100df930be7Sderaadt #define	TYPE_3		3		/* x**31 + x**3 + 1 */
101df930be7Sderaadt #define	BREAK_3		128
102df930be7Sderaadt #define	DEG_3		31
103df930be7Sderaadt #define	SEP_3		3
104df930be7Sderaadt 
105df930be7Sderaadt #define	TYPE_4		4		/* x**63 + x + 1 */
106df930be7Sderaadt #define	BREAK_4		256
107df930be7Sderaadt #define	DEG_4		63
108df930be7Sderaadt #define	SEP_4		1
109df930be7Sderaadt 
110df930be7Sderaadt /*
111df930be7Sderaadt  * Array versions of the above information to make code run faster --
112df930be7Sderaadt  * relies on fact that TYPE_i == i.
113df930be7Sderaadt  */
114df930be7Sderaadt #define	MAX_TYPES	5		/* max number of types above */
115df930be7Sderaadt 
116df930be7Sderaadt static int degrees[MAX_TYPES] =	{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
117df930be7Sderaadt static int seps [MAX_TYPES] =	{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
118df930be7Sderaadt 
119df930be7Sderaadt /*
120df930be7Sderaadt  * Initially, everything is set up as if from:
121df930be7Sderaadt  *
122df930be7Sderaadt  *	initstate(1, &randtbl, 128);
123df930be7Sderaadt  *
124df930be7Sderaadt  * Note that this initialization takes advantage of the fact that srandom()
125df930be7Sderaadt  * advances the front and rear pointers 10*rand_deg times, and hence the
126df930be7Sderaadt  * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
127df930be7Sderaadt  * element of the state information, which contains info about the current
128df930be7Sderaadt  * position of the rear pointer is just
129df930be7Sderaadt  *
130df930be7Sderaadt  *	MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3.
131df930be7Sderaadt  */
132df930be7Sderaadt 
13317ba7080Smillert static int32_t randtbl[DEG_3 + 1] = {
134df930be7Sderaadt 	TYPE_3,
13531de761cStholo 	0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05,
13631de761cStholo 	0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454,
13731de761cStholo 	0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471,
13831de761cStholo 	0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1,
13931de761cStholo 	0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41,
14031de761cStholo 	0xf3bec5da,
141df930be7Sderaadt };
142df930be7Sderaadt 
143df930be7Sderaadt /*
144df930be7Sderaadt  * fptr and rptr are two pointers into the state info, a front and a rear
145df930be7Sderaadt  * pointer.  These two pointers are always rand_sep places aparts, as they
146df930be7Sderaadt  * cycle cyclically through the state information.  (Yes, this does mean we
147df930be7Sderaadt  * could get away with just one pointer, but the code for random() is more
148df930be7Sderaadt  * efficient this way).  The pointers are left positioned as they would be
149df930be7Sderaadt  * from the call
150df930be7Sderaadt  *
151df930be7Sderaadt  *	initstate(1, randtbl, 128);
152df930be7Sderaadt  *
153df930be7Sderaadt  * (The position of the rear pointer, rptr, is really 0 (as explained above
154df930be7Sderaadt  * in the initialization of randtbl) because the state table pointer is set
155df930be7Sderaadt  * to point to randtbl[1] (as explained below).
156df930be7Sderaadt  */
15717ba7080Smillert static int32_t *fptr = &randtbl[SEP_3 + 1];
15817ba7080Smillert static int32_t *rptr = &randtbl[1];
159df930be7Sderaadt 
160df930be7Sderaadt /*
161df930be7Sderaadt  * The following things are the pointer to the state information table, the
162df930be7Sderaadt  * type of the current generator, the degree of the current polynomial being
163df930be7Sderaadt  * used, and the separation between the two pointers.  Note that for efficiency
164df930be7Sderaadt  * of random(), we remember the first location of the state information, not
165df930be7Sderaadt  * the zeroeth.  Hence it is valid to access state[-1], which is used to
166df930be7Sderaadt  * store the type of the R.N.G.  Also, we remember the last location, since
167df930be7Sderaadt  * this is more efficient than indexing every time to find the address of
168df930be7Sderaadt  * the last element to see if the front and rear pointers have wrapped.
169df930be7Sderaadt  */
17017ba7080Smillert static int32_t *state = &randtbl[1];
17117ba7080Smillert static int32_t *end_ptr = &randtbl[DEG_3 + 1];
172df930be7Sderaadt static int rand_type = TYPE_3;
173df930be7Sderaadt static int rand_deg = DEG_3;
174df930be7Sderaadt static int rand_sep = SEP_3;
175df930be7Sderaadt 
176f7510a6eSderaadt static int random_deterministic;
17700694a1dStedu 
178c6cbd355Sguenther static void *random_mutex;
179bb2902e7Stedu static long random_l(void);
180bb2902e7Stedu 
181c6cbd355Sguenther #define LOCK()		_MUTEX_LOCK(&random_mutex)
182c6cbd355Sguenther #define UNLOCK()	_MUTEX_UNLOCK(&random_mutex)
183bb2902e7Stedu 
184df930be7Sderaadt /*
185df930be7Sderaadt  * srandom:
186df930be7Sderaadt  *
187df930be7Sderaadt  * Initialize the random number generator based on the given seed.  If the
188df930be7Sderaadt  * type is the trivial no-state-information type, just remember the seed.
189df930be7Sderaadt  * Otherwise, initializes state[] based on the given "seed" via a linear
190df930be7Sderaadt  * congruential generator.  Then, the pointers are set to known locations
191df930be7Sderaadt  * that are exactly rand_sep places apart.  Lastly, it cycles the state
192df930be7Sderaadt  * information a given number of times to get rid of any initial dependencies
193df930be7Sderaadt  * introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
194df930be7Sderaadt  * for default usage relies on values produced by this routine.
195df930be7Sderaadt  */
196bb2902e7Stedu static void
srandom_l(unsigned int x)197bb2902e7Stedu srandom_l(unsigned int x)
198df930be7Sderaadt {
19917ba7080Smillert 	int i;
20017ba7080Smillert 	int32_t test;
20117ba7080Smillert 	div_t val;
202df930be7Sderaadt 
203f7510a6eSderaadt 	random_deterministic = 1;
204df930be7Sderaadt 	if (rand_type == TYPE_0)
205df930be7Sderaadt 		state[0] = x;
206df930be7Sderaadt 	else {
207ec4c3b82Smillert 		/* A seed of 0 would result in state[] always being zero. */
208ec4c3b82Smillert 		state[0] = x ? x : 1;
20931de761cStholo 		for (i = 1; i < rand_deg; i++) {
21031de761cStholo 			/*
21131de761cStholo 			 * Implement the following, without overflowing 31 bits:
21231de761cStholo 			 *
21331de761cStholo 			 *	state[i] = (16807 * state[i - 1]) % 2147483647;
21431de761cStholo 			 *
21531de761cStholo 			 *	2^31-1 (prime) = 2147483647 = 127773*16807+2836
21631de761cStholo 			 */
21717ba7080Smillert 			val = div(state[i-1], 127773);
21831de761cStholo 			test = 16807 * val.rem - 2836 * val.quot;
21931de761cStholo 			state[i] = test + (test < 0 ? 2147483647 : 0);
22031de761cStholo 		}
221df930be7Sderaadt 		fptr = &state[rand_sep];
222df930be7Sderaadt 		rptr = &state[0];
223df930be7Sderaadt 		for (i = 0; i < 10 * rand_deg; i++)
224bb2902e7Stedu 			(void)random_l();
225df930be7Sderaadt 	}
226df930be7Sderaadt }
227df930be7Sderaadt 
228bb2902e7Stedu void
srandom(unsigned int x)229bb2902e7Stedu srandom(unsigned int x)
230bb2902e7Stedu {
231f7510a6eSderaadt 	random_deterministic = 0;
232f7510a6eSderaadt }
233f7510a6eSderaadt 
234f7510a6eSderaadt void
srandomdev(void)235f7510a6eSderaadt srandomdev(void)
236f7510a6eSderaadt {
237f7510a6eSderaadt 	random_deterministic = 0;	/* back to the default */
238f7510a6eSderaadt }
239f7510a6eSderaadt 
240f7510a6eSderaadt void
srandom_deterministic(unsigned int x)241f7510a6eSderaadt srandom_deterministic(unsigned int x)
242f7510a6eSderaadt {
243bb2902e7Stedu 	LOCK();
244bb2902e7Stedu 	srandom_l(x);
245bb2902e7Stedu 	UNLOCK();
246bb2902e7Stedu }
247bb2902e7Stedu 
2489b26c4b3Smillert /*
249df930be7Sderaadt  * initstate:
250df930be7Sderaadt  *
251df930be7Sderaadt  * Initialize the state information in the given array of n bytes for future
252df930be7Sderaadt  * random number generation.  Based on the number of bytes we are given, and
253df930be7Sderaadt  * the break values for the different R.N.G.'s, we choose the best (largest)
254df930be7Sderaadt  * one we can and set things up for it.  srandom() is then called to
255df930be7Sderaadt  * initialize the state information.
256df930be7Sderaadt  *
257df930be7Sderaadt  * Note that on return from srandom(), we set state[-1] to be the type
258df930be7Sderaadt  * multiplexed with the current value of the rear pointer; this is so
259df930be7Sderaadt  * successive calls to initstate() won't lose this information and will be
260df930be7Sderaadt  * able to restart with setstate().
261df930be7Sderaadt  *
262df930be7Sderaadt  * Note: the first thing we do is save the current state, if any, just like
263df930be7Sderaadt  * setstate() so that it doesn't matter when initstate is called.
264df930be7Sderaadt  *
265df930be7Sderaadt  * Returns a pointer to the old state.
266df930be7Sderaadt  */
267df930be7Sderaadt char *
initstate(u_int seed,char * arg_state,size_t n)268d8bc04e4Spat initstate(u_int seed, char *arg_state, size_t n)
269df930be7Sderaadt {
27017ba7080Smillert 	char *ostate = (char *)(&state[-1]);
271df930be7Sderaadt 
272bb2902e7Stedu 	LOCK();
273f7510a6eSderaadt 	random_deterministic = 1;
274df930be7Sderaadt 	if (rand_type == TYPE_0)
275df930be7Sderaadt 		state[-1] = rand_type;
276df930be7Sderaadt 	else
277df930be7Sderaadt 		state[-1] = MAX_TYPES * (rptr - state) + rand_type;
278bb2902e7Stedu 	if (n < BREAK_0) {
279bb2902e7Stedu 		UNLOCK();
2804cf951c0Smillert 		return(NULL);
281bb2902e7Stedu 	}
282df930be7Sderaadt 	if (n < BREAK_1) {
283df930be7Sderaadt 		rand_type = TYPE_0;
284df930be7Sderaadt 		rand_deg = DEG_0;
285df930be7Sderaadt 		rand_sep = SEP_0;
286df930be7Sderaadt 	} else if (n < BREAK_2) {
287df930be7Sderaadt 		rand_type = TYPE_1;
288df930be7Sderaadt 		rand_deg = DEG_1;
289df930be7Sderaadt 		rand_sep = SEP_1;
290df930be7Sderaadt 	} else if (n < BREAK_3) {
291df930be7Sderaadt 		rand_type = TYPE_2;
292df930be7Sderaadt 		rand_deg = DEG_2;
293df930be7Sderaadt 		rand_sep = SEP_2;
294df930be7Sderaadt 	} else if (n < BREAK_4) {
295df930be7Sderaadt 		rand_type = TYPE_3;
296df930be7Sderaadt 		rand_deg = DEG_3;
297df930be7Sderaadt 		rand_sep = SEP_3;
298df930be7Sderaadt 	} else {
299df930be7Sderaadt 		rand_type = TYPE_4;
300df930be7Sderaadt 		rand_deg = DEG_4;
301df930be7Sderaadt 		rand_sep = SEP_4;
302df930be7Sderaadt 	}
30317ba7080Smillert 	state = &(((int32_t *)arg_state)[1]);	/* first location */
304df930be7Sderaadt 	end_ptr = &state[rand_deg];	/* must set end_ptr before srandom */
305bb2902e7Stedu 	srandom_l(seed);
306df930be7Sderaadt 	if (rand_type == TYPE_0)
307df930be7Sderaadt 		state[-1] = rand_type;
308df930be7Sderaadt 	else
309df930be7Sderaadt 		state[-1] = MAX_TYPES*(rptr - state) + rand_type;
310bb2902e7Stedu 	UNLOCK();
311df930be7Sderaadt 	return(ostate);
312df930be7Sderaadt }
313df930be7Sderaadt 
314df930be7Sderaadt /*
315df930be7Sderaadt  * setstate:
316df930be7Sderaadt  *
317df930be7Sderaadt  * Restore the state from the given state array.
318df930be7Sderaadt  *
319df930be7Sderaadt  * Note: it is important that we also remember the locations of the pointers
320df930be7Sderaadt  * in the current state information, and restore the locations of the pointers
321df930be7Sderaadt  * from the old state information.  This is done by multiplexing the pointer
322df930be7Sderaadt  * location into the zeroeth word of the state information.
323df930be7Sderaadt  *
324df930be7Sderaadt  * Note that due to the order in which things are done, it is OK to call
325df930be7Sderaadt  * setstate() with the same state as the current state.
326df930be7Sderaadt  *
327df930be7Sderaadt  * Returns a pointer to the old state information.
328df930be7Sderaadt  */
329df930be7Sderaadt char *
setstate(char * arg_state)330b10cd188Sguenther setstate(char *arg_state)
331df930be7Sderaadt {
33217ba7080Smillert 	int32_t *new_state = (int32_t *)arg_state;
33317ba7080Smillert 	int32_t type = new_state[0] % MAX_TYPES;
33417ba7080Smillert 	int32_t rear = new_state[0] / MAX_TYPES;
335df930be7Sderaadt 	char *ostate = (char *)(&state[-1]);
336df930be7Sderaadt 
337bb2902e7Stedu 	LOCK();
338f7510a6eSderaadt 	random_deterministic = 1;
339df930be7Sderaadt 	if (rand_type == TYPE_0)
340df930be7Sderaadt 		state[-1] = rand_type;
341df930be7Sderaadt 	else
342df930be7Sderaadt 		state[-1] = MAX_TYPES * (rptr - state) + rand_type;
343df930be7Sderaadt 	switch(type) {
344df930be7Sderaadt 	case TYPE_0:
345df930be7Sderaadt 	case TYPE_1:
346df930be7Sderaadt 	case TYPE_2:
347df930be7Sderaadt 	case TYPE_3:
348df930be7Sderaadt 	case TYPE_4:
349df930be7Sderaadt 		rand_type = type;
350df930be7Sderaadt 		rand_deg = degrees[type];
351df930be7Sderaadt 		rand_sep = seps[type];
352df930be7Sderaadt 		break;
353df930be7Sderaadt 	default:
354bb2902e7Stedu 		UNLOCK();
3554cf951c0Smillert 		return(NULL);
356df930be7Sderaadt 	}
357df930be7Sderaadt 	state = &new_state[1];
358df930be7Sderaadt 	if (rand_type != TYPE_0) {
359df930be7Sderaadt 		rptr = &state[rear];
360df930be7Sderaadt 		fptr = &state[(rear + rand_sep) % rand_deg];
361df930be7Sderaadt 	}
362df930be7Sderaadt 	end_ptr = &state[rand_deg];		/* set end_ptr too */
363bb2902e7Stedu 	UNLOCK();
364df930be7Sderaadt 	return(ostate);
365df930be7Sderaadt }
366df930be7Sderaadt 
367df930be7Sderaadt /*
368df930be7Sderaadt  * random:
369df930be7Sderaadt  *
370df930be7Sderaadt  * If we are using the trivial TYPE_0 R.N.G., just do the old linear
371df930be7Sderaadt  * congruential bit.  Otherwise, we do our fancy trinomial stuff, which is
372df930be7Sderaadt  * the same in all the other cases due to all the global variables that have
373df930be7Sderaadt  * been set up.  The basic operation is to add the number at the rear pointer
374df930be7Sderaadt  * into the one at the front pointer.  Then both pointers are advanced to
375df930be7Sderaadt  * the next location cyclically in the table.  The value returned is the sum
376df930be7Sderaadt  * generated, reduced to 31 bits by throwing away the "least random" low bit.
377df930be7Sderaadt  *
378df930be7Sderaadt  * Note: the code takes advantage of the fact that both the front and
379df930be7Sderaadt  * rear pointers can't wrap on the same call by not testing the rear
380df930be7Sderaadt  * pointer if the front one has wrapped.
381df930be7Sderaadt  *
382df930be7Sderaadt  * Returns a 31-bit random number.
383df930be7Sderaadt  */
384bb2902e7Stedu static long
random_l(void)385bb2902e7Stedu random_l(void)
386df930be7Sderaadt {
38717ba7080Smillert 	int32_t i;
388df930be7Sderaadt 
389f7510a6eSderaadt 	if (random_deterministic == 0)
39000694a1dStedu 		return arc4random() & 0x7fffffff;
39100694a1dStedu 
392df930be7Sderaadt 	if (rand_type == TYPE_0)
393df930be7Sderaadt 		i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff;
394df930be7Sderaadt 	else {
395df930be7Sderaadt 		*fptr += *rptr;
396df930be7Sderaadt 		i = (*fptr >> 1) & 0x7fffffff;	/* chucking least random bit */
397df930be7Sderaadt 		if (++fptr >= end_ptr) {
398df930be7Sderaadt 			fptr = state;
399df930be7Sderaadt 			++rptr;
400df930be7Sderaadt 		} else if (++rptr >= end_ptr)
401df930be7Sderaadt 			rptr = state;
402df930be7Sderaadt 	}
40317ba7080Smillert 	return((long)i);
404df930be7Sderaadt }
405bb2902e7Stedu 
406bb2902e7Stedu long
random(void)407bb2902e7Stedu random(void)
408bb2902e7Stedu {
409bb2902e7Stedu 	long r;
410bb2902e7Stedu 	LOCK();
411bb2902e7Stedu 	r = random_l();
412bb2902e7Stedu 	UNLOCK();
413bb2902e7Stedu 	return r;
414bb2902e7Stedu }
415de928067Sderaadt 
416de928067Sderaadt #if defined(APIWARN)
4170e6cb3cfSderaadt __warn_references(random,
418*e34cb67cStb     "random() may return deterministic values, is that what you want?");
419de928067Sderaadt #endif
420