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