xref: /illumos-gate/usr/src/lib/libc/port/gen/random.c (revision 0293487c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*0293487cSraf 
237c478bd9Sstevel@tonic-gate /*
24*0293487cSraf  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "synonyms.h"
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
46*0293487cSraf #include <string.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <limits.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * random.c:
527c478bd9Sstevel@tonic-gate  * An improved random number generation package.  In addition to the standard
537c478bd9Sstevel@tonic-gate  * rand()/srand() like interface, this package also has a special state info
547c478bd9Sstevel@tonic-gate  * interface.  The initstate() routine is called with a seed, an array of
557c478bd9Sstevel@tonic-gate  * bytes, and a count of how many bytes are being passed in; this array is then
567c478bd9Sstevel@tonic-gate  * initialized to contain information for random number generation with that
577c478bd9Sstevel@tonic-gate  * much state information.  Good sizes for the amount of state information are
587c478bd9Sstevel@tonic-gate  * 32, 64, 128, and 256 bytes.  The state can be switched by calling the
597c478bd9Sstevel@tonic-gate  * setstate() routine with the same array as was initiallized with initstate().
607c478bd9Sstevel@tonic-gate  * By default, the package runs with 128 bytes of state information and
617c478bd9Sstevel@tonic-gate  * generates far better random numbers than a linear congruential generator.
627c478bd9Sstevel@tonic-gate  * If the amount of state information is less than 32 bytes, a simple linear
637c478bd9Sstevel@tonic-gate  * congruential R.N.G. is used.
647c478bd9Sstevel@tonic-gate  * Internally, the state information is treated as an array of ints; the
657c478bd9Sstevel@tonic-gate  * zeroeth element of the array is the type of R.N.G. being used (small
667c478bd9Sstevel@tonic-gate  * integer); the remainder of the array is the state information for the
677c478bd9Sstevel@tonic-gate  * R.N.G.  Thus, 32 bytes of state information will give 7 ints worth of
687c478bd9Sstevel@tonic-gate  * state information, which will allow a degree seven polynomial.  (Note: the
697c478bd9Sstevel@tonic-gate  * zeroeth word of state information also has some other information stored
707c478bd9Sstevel@tonic-gate  * in it -- see setstate() for details).
717c478bd9Sstevel@tonic-gate  * The random number generation technique is a linear feedback shift register
727c478bd9Sstevel@tonic-gate  * approach, employing trinomials (since there are fewer terms to sum up that
737c478bd9Sstevel@tonic-gate  * way).  In this approach, the least significant bit of all the numbers in
747c478bd9Sstevel@tonic-gate  * the state table will act as a linear feedback shift register, and will have
757c478bd9Sstevel@tonic-gate  * period 2^deg - 1 (where deg is the degree of the polynomial being used,
767c478bd9Sstevel@tonic-gate  * assuming that the polynomial is irreducible and primitive).  The higher
777c478bd9Sstevel@tonic-gate  * order bits will have longer periods, since their values are also influenced
787c478bd9Sstevel@tonic-gate  * by pseudo-random carries out of the lower bits.  The total period of the
797c478bd9Sstevel@tonic-gate  * generator is approximately deg*(2**deg - 1); thus doubling the amount of
807c478bd9Sstevel@tonic-gate  * state information has a vast influence on the period of the generator.
817c478bd9Sstevel@tonic-gate  * Note: the deg*(2**deg - 1) is an approximation only good for large deg,
827c478bd9Sstevel@tonic-gate  * when the period of the shift register is the dominant factor.  With deg
837c478bd9Sstevel@tonic-gate  * equal to seven, the period is actually much longer than the 7*(2**7 - 1)
847c478bd9Sstevel@tonic-gate  * predicted by this formula.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * For each of the currently supported random number generators, we have a
917c478bd9Sstevel@tonic-gate  * break value on the amount of state information (you need at least this
927c478bd9Sstevel@tonic-gate  * many bytes of state info to support this random number generator), a degree
937c478bd9Sstevel@tonic-gate  * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
947c478bd9Sstevel@tonic-gate  * the separation between the two lower order coefficients of the trinomial.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define		TYPE_0		0		/* linear congruential */
987c478bd9Sstevel@tonic-gate #define		BREAK_0		8
997c478bd9Sstevel@tonic-gate #define		DEG_0		0
1007c478bd9Sstevel@tonic-gate #define		SEP_0		0
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define		TYPE_1		1		/* x**7 + x**3 + 1 */
1037c478bd9Sstevel@tonic-gate #define		BREAK_1		32
1047c478bd9Sstevel@tonic-gate #define		DEG_1		7
1057c478bd9Sstevel@tonic-gate #define		SEP_1		3
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define		TYPE_2		2		/* x**15 + x + 1 */
1087c478bd9Sstevel@tonic-gate #define		BREAK_2		64
1097c478bd9Sstevel@tonic-gate #define		DEG_2		15
1107c478bd9Sstevel@tonic-gate #define		SEP_2		1
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define		TYPE_3		3		/* x**31 + x**3 + 1 */
1137c478bd9Sstevel@tonic-gate #define		BREAK_3		128
1147c478bd9Sstevel@tonic-gate #define		DEG_3		31
1157c478bd9Sstevel@tonic-gate #define		SEP_3		3
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #define		TYPE_4		4		/* x**63 + x + 1 */
1187c478bd9Sstevel@tonic-gate #define		BREAK_4		256
1197c478bd9Sstevel@tonic-gate #define		DEG_4		63
1207c478bd9Sstevel@tonic-gate #define		SEP_4		1
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Array versions of the above information to make code run faster -- relies
1257c478bd9Sstevel@tonic-gate  * on fact that TYPE_i == i.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define		MAX_TYPES	5		/* max number of types above */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static struct _randomjunk {
1317c478bd9Sstevel@tonic-gate 	unsigned int	degrees[MAX_TYPES];
1327c478bd9Sstevel@tonic-gate 	unsigned int	seps[MAX_TYPES];
1337c478bd9Sstevel@tonic-gate 	unsigned int	randtbl[ DEG_3 + 1 ];
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * fptr and rptr are two pointers into the state info, a front and a rear
1367c478bd9Sstevel@tonic-gate  * pointer.  These two pointers are always rand_sep places aparts, as they cycle
1377c478bd9Sstevel@tonic-gate  * cyclically through the state information.  (Yes, this does mean we could get
1387c478bd9Sstevel@tonic-gate  * away with just one pointer, but the code for random() is more efficient this
1397c478bd9Sstevel@tonic-gate  * way).  The pointers are left positioned as they would be from the call
1407c478bd9Sstevel@tonic-gate  *			initstate( 1, randtbl, 128 )
1417c478bd9Sstevel@tonic-gate  * (The position of the rear pointer, rptr, is really 0 (as explained above
1427c478bd9Sstevel@tonic-gate  * in the initialization of randtbl) because the state table pointer is set
1437c478bd9Sstevel@tonic-gate  * to point to randtbl[1] (as explained below).
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate 	unsigned int	*fptr, *rptr;
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * The following things are the pointer to the state information table,
1487c478bd9Sstevel@tonic-gate  * the type of the current generator, the degree of the current polynomial
1497c478bd9Sstevel@tonic-gate  * being used, and the separation between the two pointers.
1507c478bd9Sstevel@tonic-gate  * Note that for efficiency of random(), we remember the first location of
1517c478bd9Sstevel@tonic-gate  * the state information, not the zeroeth.  Hence it is valid to access
1527c478bd9Sstevel@tonic-gate  * state[-1], which is used to store the type of the R.N.G.
1537c478bd9Sstevel@tonic-gate  * Also, we remember the last location, since this is more efficient than
1547c478bd9Sstevel@tonic-gate  * indexing every time to find the address of the last element to see if
1557c478bd9Sstevel@tonic-gate  * the front and rear pointers have wrapped.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate 	unsigned int	*state;
1587c478bd9Sstevel@tonic-gate 	unsigned int	rand_type, rand_deg, rand_sep;
1597c478bd9Sstevel@tonic-gate 	unsigned int	*end_ptr;
1607c478bd9Sstevel@tonic-gate } *__randomjunk, *_randomjunk(void), _randominit = {
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * Initially, everything is set up as if from :
1637c478bd9Sstevel@tonic-gate 	 *		initstate( 1, &randtbl, 128 );
1647c478bd9Sstevel@tonic-gate 	 * Note that this initialization takes advantage of the fact
1657c478bd9Sstevel@tonic-gate 	 * that srandom() advances the front and rear pointers 10*rand_deg
1667c478bd9Sstevel@tonic-gate 	 * times, and hence the rear pointer which starts at 0 will also
1677c478bd9Sstevel@tonic-gate 	 * end up at zero; thus the zeroeth element of the state
1687c478bd9Sstevel@tonic-gate 	 * information, which contains info about the current
1697c478bd9Sstevel@tonic-gate 	 * position of the rear pointer is just
1707c478bd9Sstevel@tonic-gate 	 *	MAX_TYPES*(rptr - state) + TYPE_3 == TYPE_3.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 },
1737c478bd9Sstevel@tonic-gate 	{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 },
1747c478bd9Sstevel@tonic-gate 	{ TYPE_3,
1757c478bd9Sstevel@tonic-gate 	    0x9a319039U, 0x32d9c024U, 0x9b663182U, 0x5da1f342U,
1767c478bd9Sstevel@tonic-gate 	    0xde3b81e0U, 0xdf0a6fb5U, 0xf103bc02U, 0x48f340fbU,
1777c478bd9Sstevel@tonic-gate 	    0x7449e56bU, 0xbeb1dbb0U, 0xab5c5918U, 0x946554fdU,
1787c478bd9Sstevel@tonic-gate 	    0x8c2e680fU, 0xeb3d799fU, 0xb11ee0b7U, 0x2d436b86U,
1797c478bd9Sstevel@tonic-gate 	    0xda672e2aU, 0x1588ca88U, 0xe369735dU, 0x904f35f7U,
1807c478bd9Sstevel@tonic-gate 	    0xd7158fd6U, 0x6fa6f051U, 0x616e6b96U, 0xac94efdcU,
1817c478bd9Sstevel@tonic-gate 	    0x36413f93U, 0xc622c298U, 0xf5a42ab8U, 0x8a88d77bU,
1827c478bd9Sstevel@tonic-gate 			0xf5ad9d0eU, 0x8999220bU, 0x27fb47b9U },
1837c478bd9Sstevel@tonic-gate 	&_randominit.randtbl[ SEP_3 + 1 ],
1847c478bd9Sstevel@tonic-gate 	&_randominit.randtbl[ 1 ],
1857c478bd9Sstevel@tonic-gate 	&_randominit.randtbl[ 1 ],
1867c478bd9Sstevel@tonic-gate 	TYPE_3, DEG_3, SEP_3,
1877c478bd9Sstevel@tonic-gate 	&_randominit.randtbl[ DEG_3 + 1]
1887c478bd9Sstevel@tonic-gate };
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static struct _randomjunk *
1917c478bd9Sstevel@tonic-gate _randomjunk(void)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	struct _randomjunk *rp = __randomjunk;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
1967c478bd9Sstevel@tonic-gate 		rp = (struct _randomjunk *)malloc(sizeof (*rp));
1977c478bd9Sstevel@tonic-gate 		if (rp == NULL)
1987c478bd9Sstevel@tonic-gate 			return (NULL);
199*0293487cSraf 		(void) memcpy(rp, &_randominit, sizeof (*rp));
2007c478bd9Sstevel@tonic-gate 		__randomjunk = rp;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	return (rp);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * initstate:
2087c478bd9Sstevel@tonic-gate  * Initialize the state information in the given array of n bytes for
2097c478bd9Sstevel@tonic-gate  * future random number generation.  Based on the number of bytes we
2107c478bd9Sstevel@tonic-gate  * are given, and the break values for the different R.N.G.'s, we choose
2117c478bd9Sstevel@tonic-gate  * the best (largest) one we can and set things up for it.  srandom() is
2127c478bd9Sstevel@tonic-gate  * then called to initialize the state information.
2137c478bd9Sstevel@tonic-gate  * Note that on return from srandom(), we set state[-1] to be the type
2147c478bd9Sstevel@tonic-gate  * multiplexed with the current value of the rear pointer; this is so
2157c478bd9Sstevel@tonic-gate  * successive calls to initstate() won't lose this information and will
2167c478bd9Sstevel@tonic-gate  * be able to restart with setstate().
2177c478bd9Sstevel@tonic-gate  * Note: the first thing we do is save the current state, if any, just like
2187c478bd9Sstevel@tonic-gate  * setstate() so that it doesn't matter when initstate is called.
2197c478bd9Sstevel@tonic-gate  * Returns a pointer to the old state.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate char  *
2237c478bd9Sstevel@tonic-gate initstate(
2247c478bd9Sstevel@tonic-gate 	unsigned int seed,	/* seed for R. N. G. */
2257c478bd9Sstevel@tonic-gate 	char *arg_state,	/* pointer to state array */
2267c478bd9Sstevel@tonic-gate 	size_t size)		/* # bytes of state info */
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	unsigned int n;
2297c478bd9Sstevel@tonic-gate 	struct _randomjunk *rp = _randomjunk();
2307c478bd9Sstevel@tonic-gate 	char		*ostate;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (size > UINT_MAX)
2337c478bd9Sstevel@tonic-gate 		n = UINT_MAX;
2347c478bd9Sstevel@tonic-gate 	else
2357c478bd9Sstevel@tonic-gate 		n = (unsigned int)size;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (rp == NULL)
2387c478bd9Sstevel@tonic-gate 		return (NULL);
2397c478bd9Sstevel@tonic-gate 	ostate = (char *)(&rp->state[ -1 ]);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (rp->rand_type  ==  TYPE_0)  rp->state[ -1 ] = rp->rand_type;
2427c478bd9Sstevel@tonic-gate 	else  rp->state[ -1 ] =
2437c478bd9Sstevel@tonic-gate 	    (unsigned int)(MAX_TYPES*(rp->rptr - rp->state) + rp->rand_type);
2447c478bd9Sstevel@tonic-gate 	if (n  <  BREAK_1)  {
2457c478bd9Sstevel@tonic-gate 	    if (n  <  BREAK_0)  {
2467c478bd9Sstevel@tonic-gate 		return (NULL);
2477c478bd9Sstevel@tonic-gate 	    }
2487c478bd9Sstevel@tonic-gate 	    rp->rand_type = TYPE_0;
2497c478bd9Sstevel@tonic-gate 	    rp->rand_deg = DEG_0;
2507c478bd9Sstevel@tonic-gate 	    rp->rand_sep = SEP_0;
2517c478bd9Sstevel@tonic-gate 	} else  {
2527c478bd9Sstevel@tonic-gate 	    if (n  <  BREAK_2)  {
2537c478bd9Sstevel@tonic-gate 		rp->rand_type = TYPE_1;
2547c478bd9Sstevel@tonic-gate 		rp->rand_deg = DEG_1;
2557c478bd9Sstevel@tonic-gate 		rp->rand_sep = SEP_1;
2567c478bd9Sstevel@tonic-gate 	    } else  {
2577c478bd9Sstevel@tonic-gate 		if (n  <  BREAK_3)  {
2587c478bd9Sstevel@tonic-gate 		    rp->rand_type = TYPE_2;
2597c478bd9Sstevel@tonic-gate 		    rp->rand_deg = DEG_2;
2607c478bd9Sstevel@tonic-gate 		    rp->rand_sep = SEP_2;
2617c478bd9Sstevel@tonic-gate 		} else  {
2627c478bd9Sstevel@tonic-gate 		    if (n  <  BREAK_4)  {
2637c478bd9Sstevel@tonic-gate 			rp->rand_type = TYPE_3;
2647c478bd9Sstevel@tonic-gate 			rp->rand_deg = DEG_3;
2657c478bd9Sstevel@tonic-gate 			rp->rand_sep = SEP_3;
2667c478bd9Sstevel@tonic-gate 		    } else  {
2677c478bd9Sstevel@tonic-gate 			rp->rand_type = TYPE_4;
2687c478bd9Sstevel@tonic-gate 			rp->rand_deg = DEG_4;
2697c478bd9Sstevel@tonic-gate 			rp->rand_sep = SEP_4;
2707c478bd9Sstevel@tonic-gate 		    }
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 	    }
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	/* first location */
2757c478bd9Sstevel@tonic-gate 	rp->state = &(((unsigned int *)(uintptr_t)arg_state)[1]);
2767c478bd9Sstevel@tonic-gate 	/* must set end_ptr before srandom */
2777c478bd9Sstevel@tonic-gate 	rp->end_ptr = &rp->state[rp->rand_deg];
2787c478bd9Sstevel@tonic-gate 	srandom(seed);
2797c478bd9Sstevel@tonic-gate 	if (rp->rand_type  ==  TYPE_0)  rp->state[ -1 ] = rp->rand_type;
2807c478bd9Sstevel@tonic-gate 	else
2817c478bd9Sstevel@tonic-gate 		rp->state[-1] = (unsigned int)(MAX_TYPES*
2827c478bd9Sstevel@tonic-gate 		    (rp->rptr - rp->state) + rp->rand_type);
2837c478bd9Sstevel@tonic-gate 	return (ostate);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * setstate:
2907c478bd9Sstevel@tonic-gate  * Restore the state from the given state array.
2917c478bd9Sstevel@tonic-gate  * Note: it is important that we also remember the locations of the pointers
2927c478bd9Sstevel@tonic-gate  * in the current state information, and restore the locations of the pointers
2937c478bd9Sstevel@tonic-gate  * from the old state information.  This is done by multiplexing the pointer
2947c478bd9Sstevel@tonic-gate  * location into the zeroeth word of the state information.
2957c478bd9Sstevel@tonic-gate  * Note that due to the order in which things are done, it is OK to call
2967c478bd9Sstevel@tonic-gate  * setstate() with the same state as the current state.
2977c478bd9Sstevel@tonic-gate  * Returns a pointer to the old state information.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate char  *
3017c478bd9Sstevel@tonic-gate setstate(const char *arg_state)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	struct _randomjunk *rp = _randomjunk();
3047c478bd9Sstevel@tonic-gate 	unsigned int	*new_state;
3057c478bd9Sstevel@tonic-gate 	unsigned int	type;
3067c478bd9Sstevel@tonic-gate 	unsigned int	rear;
3077c478bd9Sstevel@tonic-gate 	char		*ostate;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (rp == NULL)
3107c478bd9Sstevel@tonic-gate 		return (NULL);
3117c478bd9Sstevel@tonic-gate 	new_state = (unsigned int *)(uintptr_t)arg_state;
3127c478bd9Sstevel@tonic-gate 	type = new_state[0]%MAX_TYPES;
3137c478bd9Sstevel@tonic-gate 	rear = new_state[0]/MAX_TYPES;
3147c478bd9Sstevel@tonic-gate 	ostate = (char *)(&rp->state[ -1 ]);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (rp->rand_type  ==  TYPE_0) rp->state[ -1 ] = rp->rand_type;
3177c478bd9Sstevel@tonic-gate 	else
3187c478bd9Sstevel@tonic-gate 		rp->state[-1] = (unsigned int)(MAX_TYPES*
3197c478bd9Sstevel@tonic-gate 		    (rp->rptr - rp->state) + rp->rand_type);
3207c478bd9Sstevel@tonic-gate 	switch (type)  {
3217c478bd9Sstevel@tonic-gate 	    case  TYPE_0:
3227c478bd9Sstevel@tonic-gate 	    case  TYPE_1:
3237c478bd9Sstevel@tonic-gate 	    case  TYPE_2:
3247c478bd9Sstevel@tonic-gate 	    case  TYPE_3:
3257c478bd9Sstevel@tonic-gate 	    case  TYPE_4:
3267c478bd9Sstevel@tonic-gate 		rp->rand_type = type;
3277c478bd9Sstevel@tonic-gate 		rp->rand_deg = rp->degrees[ type ];
3287c478bd9Sstevel@tonic-gate 		rp->rand_sep = rp->seps[ type ];
3297c478bd9Sstevel@tonic-gate 		break;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	    default:
3327c478bd9Sstevel@tonic-gate 		return (NULL);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	rp->state = &new_state[ 1 ];
3357c478bd9Sstevel@tonic-gate 	if (rp->rand_type  !=  TYPE_0)  {
3367c478bd9Sstevel@tonic-gate 	    rp->rptr = &rp->state[ rear ];
3377c478bd9Sstevel@tonic-gate 	    rp->fptr = &rp->state[ (rear + rp->rand_sep)%rp->rand_deg ];
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	rp->end_ptr = &rp->state[ rp->rand_deg ];	/* set end_ptr too */
3407c478bd9Sstevel@tonic-gate 	return (ostate);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * random:
3477c478bd9Sstevel@tonic-gate  * If we are using the trivial TYPE_0 R.N.G., just do the old linear
3487c478bd9Sstevel@tonic-gate  * congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
3497c478bd9Sstevel@tonic-gate  * same in all ther other cases due to all the global variables that have been
3507c478bd9Sstevel@tonic-gate  * set up.  The basic operation is to add the number at the rear pointer into
3517c478bd9Sstevel@tonic-gate  * the one at the front pointer.  Then both pointers are advanced to the next
3527c478bd9Sstevel@tonic-gate  * location cyclically in the table.  The value returned is the sum generated,
3537c478bd9Sstevel@tonic-gate  * reduced to 31 bits by throwing away the "least random" low bit.
3547c478bd9Sstevel@tonic-gate  * Note: the code takes advantage of the fact that both the front and
3557c478bd9Sstevel@tonic-gate  * rear pointers can't wrap on the same call by not testing the rear
3567c478bd9Sstevel@tonic-gate  * pointer if the front one has wrapped.
3577c478bd9Sstevel@tonic-gate  * Returns a 31-bit random number.
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate long
3617c478bd9Sstevel@tonic-gate random(void)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	struct _randomjunk *rp = _randomjunk();
3647c478bd9Sstevel@tonic-gate 	unsigned int	i;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (rp == NULL)
3677c478bd9Sstevel@tonic-gate 		return (0L);
3687c478bd9Sstevel@tonic-gate 	if (rp->rand_type  ==  TYPE_0)  {
3697c478bd9Sstevel@tonic-gate 	    i = rp->state[0] = (rp->state[0]*1103515245 + 12345)&0x7fffffff;
3707c478bd9Sstevel@tonic-gate 	} else  {
3717c478bd9Sstevel@tonic-gate 	    *rp->fptr += *rp->rptr;
3727c478bd9Sstevel@tonic-gate 	    i = (*rp->fptr >> 1)&0x7fffffff;	/* chucking least random bit */
3737c478bd9Sstevel@tonic-gate 	    if (++rp->fptr  >=  rp->end_ptr)  {
3747c478bd9Sstevel@tonic-gate 		rp->fptr = rp->state;
3757c478bd9Sstevel@tonic-gate 		++rp->rptr;
3767c478bd9Sstevel@tonic-gate 	    } else  {
3777c478bd9Sstevel@tonic-gate 		if (++rp->rptr  >=  rp->end_ptr)  rp->rptr = rp->state;
3787c478bd9Sstevel@tonic-gate 	    }
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	return ((long)i);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * srandom:
3857c478bd9Sstevel@tonic-gate  * Initialize the random number generator based on the given seed.  If the
3867c478bd9Sstevel@tonic-gate  * type is the trivial no-state-information type, just remember the seed.
3877c478bd9Sstevel@tonic-gate  * Otherwise, initializes state[] based on the given "seed" via a linear
3887c478bd9Sstevel@tonic-gate  * congruential generator.  Then, the pointers are set to known locations
3897c478bd9Sstevel@tonic-gate  * that are exactly rand_sep places apart.  Lastly, it cycles the state
3907c478bd9Sstevel@tonic-gate  * information a given number of times to get rid of any initial dependencies
3917c478bd9Sstevel@tonic-gate  * introduced by the L.C.R.N.G.
3927c478bd9Sstevel@tonic-gate  * Note that the initialization of randtbl[] for default usage relies on
3937c478bd9Sstevel@tonic-gate  * values produced by this routine.
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate void
3977c478bd9Sstevel@tonic-gate srandom(unsigned int x)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	struct _randomjunk *rp = _randomjunk();
4007c478bd9Sstevel@tonic-gate 	unsigned int	i;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (rp == NULL)
4037c478bd9Sstevel@tonic-gate 		return;
4047c478bd9Sstevel@tonic-gate 	if (rp->rand_type  ==  TYPE_0)  {
4057c478bd9Sstevel@tonic-gate 	    rp->state[ 0 ] = x;
4067c478bd9Sstevel@tonic-gate 	} else  {
4077c478bd9Sstevel@tonic-gate 	    rp->state[ 0 ] = x;
4087c478bd9Sstevel@tonic-gate 	    for (i = 1; i < rp->rand_deg; i++)  {
4097c478bd9Sstevel@tonic-gate 		rp->state[i] = 1103515245*rp->state[i - 1] + 12345;
4107c478bd9Sstevel@tonic-gate 	    }
4117c478bd9Sstevel@tonic-gate 	    rp->fptr = &rp->state[ rp->rand_sep ];
4127c478bd9Sstevel@tonic-gate 	    rp->rptr = &rp->state[ 0 ];
4137c478bd9Sstevel@tonic-gate 	    for (i = 0; i < 10*rp->rand_deg; i++)  (void)random();
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate }
416