xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/random.c (revision 723:28842cf1cc1a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23722Smuffin  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
24722Smuffin  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29722Smuffin #include <stdio.h>
30722Smuffin #include <stdlib.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * random.c:
340Sstevel@tonic-gate  * An improved random number generation package.  In addition to the standard
350Sstevel@tonic-gate  * rand()/srand() like interface, this package also has a special state info
360Sstevel@tonic-gate  * interface.  The initstate() routine is called with a seed, an array of
370Sstevel@tonic-gate  * bytes, and a count of how many bytes are being passed in; this array is then
380Sstevel@tonic-gate  * initialized to contain information for random number generation with that
390Sstevel@tonic-gate  * much state information.  Good sizes for the amount of state information are
400Sstevel@tonic-gate  * 32, 64, 128, and 256 bytes.  The state can be switched by calling the
410Sstevel@tonic-gate  * setstate() routine with the same array as was initiallized with initstate().
420Sstevel@tonic-gate  * By default, the package runs with 128 bytes of state information and
430Sstevel@tonic-gate  * generates far better random numbers than a linear congruential generator.
440Sstevel@tonic-gate  * If the amount of state information is less than 32 bytes, a simple linear
450Sstevel@tonic-gate  * congruential R.N.G. is used.
460Sstevel@tonic-gate  * Internally, the state information is treated as an array of longs; the
470Sstevel@tonic-gate  * zeroeth element of the array is the type of R.N.G. being used (small
480Sstevel@tonic-gate  * integer); the remainder of the array is the state information for the
490Sstevel@tonic-gate  * R.N.G.  Thus, 32 bytes of state information will give 7 longs worth of
500Sstevel@tonic-gate  * state information, which will allow a degree seven polynomial.  (Note: the
510Sstevel@tonic-gate  * zeroeth word of state information also has some other information stored
520Sstevel@tonic-gate  * in it -- see setstate() for details).
530Sstevel@tonic-gate  * The random number generation technique is a linear feedback shift register
540Sstevel@tonic-gate  * approach, employing trinomials (since there are fewer terms to sum up that
550Sstevel@tonic-gate  * way).  In this approach, the least significant bit of all the numbers in
560Sstevel@tonic-gate  * the state table will act as a linear feedback shift register, and will have
570Sstevel@tonic-gate  * period 2^deg - 1 (where deg is the degree of the polynomial being used,
580Sstevel@tonic-gate  * assuming that the polynomial is irreducible and primitive).  The higher
590Sstevel@tonic-gate  * order bits will have longer periods, since their values are also influenced
600Sstevel@tonic-gate  * by pseudo-random carries out of the lower bits.  The total period of the
610Sstevel@tonic-gate  * generator is approximately deg*(2**deg - 1); thus doubling the amount of
620Sstevel@tonic-gate  * state information has a vast influence on the period of the generator.
630Sstevel@tonic-gate  * Note: the deg*(2**deg - 1) is an approximation only good for large deg,
640Sstevel@tonic-gate  * when the period of the shift register is the dominant factor.  With deg
650Sstevel@tonic-gate  * equal to seven, the period is actually much longer than the 7*(2**7 - 1)
660Sstevel@tonic-gate  * predicted by this formula.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * For each of the currently supported random number generators, we have a
730Sstevel@tonic-gate  * break value on the amount of state information (you need at least this
740Sstevel@tonic-gate  * many bytes of state info to support this random number generator), a degree
750Sstevel@tonic-gate  * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
760Sstevel@tonic-gate  * the separation between the two lower order coefficients of the trinomial.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define		TYPE_0		0		/* linear congruential */
800Sstevel@tonic-gate #define		BREAK_0		8
810Sstevel@tonic-gate #define		DEG_0		0
820Sstevel@tonic-gate #define		SEP_0		0
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define		TYPE_1		1		/* x**7 + x**3 + 1 */
850Sstevel@tonic-gate #define		BREAK_1		32
860Sstevel@tonic-gate #define		DEG_1		7
870Sstevel@tonic-gate #define		SEP_1		3
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define		TYPE_2		2		/* x**15 + x + 1 */
900Sstevel@tonic-gate #define		BREAK_2		64
910Sstevel@tonic-gate #define		DEG_2		15
920Sstevel@tonic-gate #define		SEP_2		1
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #define		TYPE_3		3		/* x**31 + x**3 + 1 */
950Sstevel@tonic-gate #define		BREAK_3		128
960Sstevel@tonic-gate #define		DEG_3		31
970Sstevel@tonic-gate #define		SEP_3		3
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define		TYPE_4		4		/* x**63 + x + 1 */
1000Sstevel@tonic-gate #define		BREAK_4		256
1010Sstevel@tonic-gate #define		DEG_4		63
1020Sstevel@tonic-gate #define		SEP_4		1
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Array versions of the above information to make code run faster -- relies
1070Sstevel@tonic-gate  * on fact that TYPE_i == i.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #define		MAX_TYPES	5		/* max number of types above */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static struct _randomjunk {
1130Sstevel@tonic-gate 	int	degrees[MAX_TYPES];
1140Sstevel@tonic-gate 	int	seps[MAX_TYPES];
1150Sstevel@tonic-gate 	long	randtbl[ DEG_3 + 1 ];
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * fptr and rptr are two pointers into the state info, a front and a rear
1180Sstevel@tonic-gate  * pointer.  These two pointers are always rand_sep places aparts, as they cycle
1190Sstevel@tonic-gate  * cyclically through the state information.  (Yes, this does mean we could get
1200Sstevel@tonic-gate  * away with just one pointer, but the code for random() is more efficient this
1210Sstevel@tonic-gate  * way).  The pointers are left positioned as they would be from the call
1220Sstevel@tonic-gate  *			initstate(1, randtbl, 128)
1230Sstevel@tonic-gate  * (The position of the rear pointer, rptr, is really 0 (as explained above
1240Sstevel@tonic-gate  * in the initialization of randtbl) because the state table pointer is set
1250Sstevel@tonic-gate  * to point to randtbl[1] (as explained below).
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate 	long	*fptr, *rptr;
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * The following things are the pointer to the state information table,
1300Sstevel@tonic-gate  * the type of the current generator, the degree of the current polynomial
1310Sstevel@tonic-gate  * being used, and the separation between the two pointers.
1320Sstevel@tonic-gate  * Note that for efficiency of random(), we remember the first location of
1330Sstevel@tonic-gate  * the state information, not the zeroeth.  Hence it is valid to access
1340Sstevel@tonic-gate  * state[-1], which is used to store the type of the R.N.G.
1350Sstevel@tonic-gate  * Also, we remember the last location, since this is more efficient than
1360Sstevel@tonic-gate  * indexing every time to find the address of the last element to see if
1370Sstevel@tonic-gate  * the front and rear pointers have wrapped.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate 	long	*state;
1400Sstevel@tonic-gate 	int	rand_type, rand_deg, rand_sep;
1410Sstevel@tonic-gate 	long	*end_ptr;
142722Smuffin } *__randomjunk, *_randomjunk(void), _randominit = {
1430Sstevel@tonic-gate 	/*
1440Sstevel@tonic-gate 	 * Initially, everything is set up as if from :
1450Sstevel@tonic-gate 	 *		initstate(1, &randtbl, 128);
1460Sstevel@tonic-gate 	 * Note that this initialization takes advantage of the fact
1470Sstevel@tonic-gate 	 * that srandom() advances the front and rear pointers 10*rand_deg
1480Sstevel@tonic-gate 	 * times, and hence the rear pointer which starts at 0 will also
1490Sstevel@tonic-gate 	 * end up at zero; thus the zeroeth element of the state
1500Sstevel@tonic-gate 	 * information, which contains info about the current
1510Sstevel@tonic-gate 	 * position of the rear pointer is just
1520Sstevel@tonic-gate 	 *	MAX_TYPES*(rptr - state) + TYPE_3 == TYPE_3.
1530Sstevel@tonic-gate 	 */
1540Sstevel@tonic-gate 	{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 },
1550Sstevel@tonic-gate 	{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 },
1560Sstevel@tonic-gate 	{ TYPE_3,
1570Sstevel@tonic-gate 	(long)0x9a319039, (long)0x32d9c024, (long)0x9b663182, (long)0x5da1f342,
1580Sstevel@tonic-gate 	(long)0xde3b81e0, (long)0xdf0a6fb5, (long)0xf103bc02, (long)0x48f340fb,
1590Sstevel@tonic-gate 	(long)0x7449e56b, (long)0xbeb1dbb0, (long)0xab5c5918, (long)0x946554fd,
1600Sstevel@tonic-gate 	(long)0x8c2e680f, (long)0xeb3d799f, (long)0xb11ee0b7, (long)0x2d436b86,
1610Sstevel@tonic-gate 	(long)0xda672e2a, (long)0x1588ca88, (long)0xe369735d, (long)0x904f35f7,
1620Sstevel@tonic-gate 	(long)0xd7158fd6, (long)0x6fa6f051, (long)0x616e6b96, (long)0xac94efdc,
1630Sstevel@tonic-gate 	(long)0x36413f93, (long)0xc622c298, (long)0xf5a42ab8, (long)0x8a88d77b,
1640Sstevel@tonic-gate 	(long)0xf5ad9d0e, (long)0x8999220b, (long)0x27fb47b9 },
1650Sstevel@tonic-gate 	&_randominit.randtbl[ SEP_3 + 1 ],
1660Sstevel@tonic-gate 	&_randominit.randtbl[1],
1670Sstevel@tonic-gate 	&_randominit.randtbl[1],
1680Sstevel@tonic-gate 	TYPE_3, DEG_3, SEP_3,
1690Sstevel@tonic-gate 	&_randominit.randtbl[ DEG_3 + 1]
1700Sstevel@tonic-gate };
1710Sstevel@tonic-gate 
172722Smuffin long random(void);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate static struct _randomjunk *
_randomjunk(void)175722Smuffin _randomjunk(void)
1760Sstevel@tonic-gate {
177722Smuffin 	struct _randomjunk *rp = __randomjunk;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (rp == 0) {
1800Sstevel@tonic-gate 		rp = (struct _randomjunk *)malloc(sizeof (*rp));
1810Sstevel@tonic-gate 		if (rp == 0)
1820Sstevel@tonic-gate 			return (0);
1830Sstevel@tonic-gate 		*rp = _randominit;
1840Sstevel@tonic-gate 		__randomjunk = rp;
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	return (rp);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * srandom:
1910Sstevel@tonic-gate  * Initialize the random number generator based on the given seed.  If the
1920Sstevel@tonic-gate  * type is the trivial no-state-information type, just remember the seed.
1930Sstevel@tonic-gate  * Otherwise, initializes state[] based on the given "seed" via a linear
1940Sstevel@tonic-gate  * congruential generator.  Then, the pointers are set to known locations
1950Sstevel@tonic-gate  * that are exactly rand_sep places apart.  Lastly, it cycles the state
1960Sstevel@tonic-gate  * information a given number of times to get rid of any initial dependencies
1970Sstevel@tonic-gate  * introduced by the L.C.R.N.G.
1980Sstevel@tonic-gate  * Note that the initialization of randtbl[] for default usage relies on
1990Sstevel@tonic-gate  * values produced by this routine.
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate 
202722Smuffin void
srandom(unsigned x)203722Smuffin srandom(unsigned x)
2040Sstevel@tonic-gate {
205722Smuffin 	struct _randomjunk *rp = _randomjunk();
206722Smuffin 	int		i;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (rp == 0)
2090Sstevel@tonic-gate 		return;
2100Sstevel@tonic-gate 	if (rp->rand_type  ==  TYPE_0)  {
2110Sstevel@tonic-gate 	    rp->state[0] = x;
2120Sstevel@tonic-gate 	} else  {
2130Sstevel@tonic-gate 	    rp->state[0] = x;
2140Sstevel@tonic-gate 	    for (i = 1; i < rp->rand_deg; i++)  {
2150Sstevel@tonic-gate 		rp->state[i] = 1103515245*rp->state[i - 1] + 12345;
2160Sstevel@tonic-gate 	    }
2170Sstevel@tonic-gate 	    rp->fptr = &rp->state[rp->rand_sep];
2180Sstevel@tonic-gate 	    rp->rptr = &rp->state[0];
2190Sstevel@tonic-gate 	    for (i = 0; i < 10 * rp->rand_deg; i++)
2200Sstevel@tonic-gate 		random();
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * initstate:
2280Sstevel@tonic-gate  * Initialize the state information in the given array of n bytes for
2290Sstevel@tonic-gate  * future random number generation.  Based on the number of bytes we
2300Sstevel@tonic-gate  * are given, and the break values for the different R.N.G.'s, we choose
2310Sstevel@tonic-gate  * the best (largest) one we can and set things up for it.  srandom() is
2320Sstevel@tonic-gate  * then called to initialize the state information.
2330Sstevel@tonic-gate  * Note that on return from srandom(), we set state[-1] to be the type
2340Sstevel@tonic-gate  * multiplexed with the current value of the rear pointer; this is so
2350Sstevel@tonic-gate  * successive calls to initstate() won't lose this information and will
2360Sstevel@tonic-gate  * be able to restart with setstate().
2370Sstevel@tonic-gate  * Note: the first thing we do is save the current state, if any, just like
2380Sstevel@tonic-gate  * setstate() so that it doesn't matter when initstate is called.
2390Sstevel@tonic-gate  * Returns a pointer to the old state.
240722Smuffin  *
241722Smuffin  * Arguments:
242722Smuffin  *	seed:		seed for R. N. G.
243722Smuffin  *	arg_state:	pointer to state array
244722Smuffin  *	n:		# bytes of state info
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate char  *
initstate(unsigned seed,char * arg_state,int n)248722Smuffin initstate(unsigned seed, char *arg_state, int n)
2490Sstevel@tonic-gate {
250722Smuffin 	struct _randomjunk *rp = _randomjunk();
251*723Smuffin 	char		*ostate;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (rp == 0)
2540Sstevel@tonic-gate 		return (0);
2550Sstevel@tonic-gate 	ostate = (char *)(&rp->state[-1]);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (rp->rand_type  ==  TYPE_0)  rp->state[-1] = rp->rand_type;
2580Sstevel@tonic-gate 	else  rp->state[-1] =
2590Sstevel@tonic-gate 	    MAX_TYPES*(rp->rptr - rp->state) + rp->rand_type;
2600Sstevel@tonic-gate 	if (n < BREAK_0) {
2610Sstevel@tonic-gate 		fprintf(stderr,
2620Sstevel@tonic-gate 	"initstate: state array too small, ignored; minimum size is %d bytes\n",
2630Sstevel@tonic-gate 			BREAK_0);
2640Sstevel@tonic-gate 		return (0);
2650Sstevel@tonic-gate 	} else if (n < BREAK_1) {
2660Sstevel@tonic-gate 		rp->rand_type = TYPE_0;
2670Sstevel@tonic-gate 		rp->rand_deg = DEG_0;
2680Sstevel@tonic-gate 		rp->rand_sep = SEP_0;
2690Sstevel@tonic-gate 	} else if (n < BREAK_2) {
2700Sstevel@tonic-gate 		rp->rand_type = TYPE_1;
2710Sstevel@tonic-gate 		rp->rand_deg = DEG_1;
2720Sstevel@tonic-gate 		rp->rand_sep = SEP_1;
2730Sstevel@tonic-gate 	} else if (n < BREAK_3) {
2740Sstevel@tonic-gate 		rp->rand_type = TYPE_2;
2750Sstevel@tonic-gate 		rp->rand_deg = DEG_2;
2760Sstevel@tonic-gate 		rp->rand_sep = SEP_2;
2770Sstevel@tonic-gate 	} else if (n < BREAK_4) {
2780Sstevel@tonic-gate 		rp->rand_type = TYPE_3;
2790Sstevel@tonic-gate 		rp->rand_deg = DEG_3;
2800Sstevel@tonic-gate 		rp->rand_sep = SEP_3;
2810Sstevel@tonic-gate 	} else  {
2820Sstevel@tonic-gate 		rp->rand_type = TYPE_4;
2830Sstevel@tonic-gate 		rp->rand_deg = DEG_4;
2840Sstevel@tonic-gate 		rp->rand_sep = SEP_4;
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 	rp->state = &((long *)arg_state)[1];	/* first location */
2870Sstevel@tonic-gate 	rp->end_ptr = &rp->state[rp->rand_deg];	/* set end_ptr before srandom */
2880Sstevel@tonic-gate 	srandom(seed);
2890Sstevel@tonic-gate 	rp->state[-1] = (rp->rand_type == TYPE_0) ? rp->rand_type
2900Sstevel@tonic-gate 			: MAX_TYPES * (rp->rptr - rp->state) + rp->rand_type;
2910Sstevel@tonic-gate 	return (ostate);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate  * setstate:
2970Sstevel@tonic-gate  * Restore the state from the given state array.
2980Sstevel@tonic-gate  * Note: it is important that we also remember the locations of the pointers
2990Sstevel@tonic-gate  * in the current state information, and restore the locations of the pointers
3000Sstevel@tonic-gate  * from the old state information.  This is done by multiplexing the pointer
3010Sstevel@tonic-gate  * location into the zeroeth word of the state information.
3020Sstevel@tonic-gate  * Note that due to the order in which things are done, it is OK to call
3030Sstevel@tonic-gate  * setstate() with the same state as the current state.
3040Sstevel@tonic-gate  * Returns a pointer to the old state information.
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate char  *
setstate(char * arg_state)308722Smuffin setstate(char *arg_state)
3090Sstevel@tonic-gate {
310722Smuffin 	struct _randomjunk *rp = _randomjunk();
311722Smuffin 	long		*new_state;
312722Smuffin 	int		type;
313722Smuffin 	int		rear;
3140Sstevel@tonic-gate 	char			*ostate;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (rp == 0)
3170Sstevel@tonic-gate 		return (0);
3180Sstevel@tonic-gate 	new_state = (long *)arg_state;
3190Sstevel@tonic-gate 	type = new_state[0] % MAX_TYPES;
3200Sstevel@tonic-gate 	rear = new_state[0] / MAX_TYPES;
3210Sstevel@tonic-gate 	ostate = (char *)(&rp->state[-1]);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	rp->state[-1] = (rp->rand_type == TYPE_0) ? rp->rand_type
3240Sstevel@tonic-gate 			: MAX_TYPES*(rp->rptr - rp->state) + rp->rand_type;
3250Sstevel@tonic-gate 	switch (type)  {
326722Smuffin 	case  TYPE_0:
327722Smuffin 	case  TYPE_1:
328722Smuffin 	case  TYPE_2:
329722Smuffin 	case  TYPE_3:
330722Smuffin 	case  TYPE_4:
3310Sstevel@tonic-gate 		rp->rand_type = type;
3320Sstevel@tonic-gate 		rp->rand_deg = rp->degrees[type];
3330Sstevel@tonic-gate 		rp->rand_sep = rp->seps[type];
3340Sstevel@tonic-gate 		break;
3350Sstevel@tonic-gate 
336722Smuffin 	default:
3370Sstevel@tonic-gate 		fprintf(stderr, "setstate: invalid state info; not changed.\n");
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	rp->state = &new_state[1];
3400Sstevel@tonic-gate 	if (rp->rand_type != TYPE_0)  {
3410Sstevel@tonic-gate 	    rp->rptr = &rp->state[rear];
3420Sstevel@tonic-gate 	    rp->fptr = &rp->state[(rear + rp->rand_sep) % rp->rand_deg];
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 	rp->end_ptr = &rp->state[rp->rand_deg];	/* set end_ptr too */
3450Sstevel@tonic-gate 	return (ostate);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * random:
3510Sstevel@tonic-gate  * If we are using the trivial TYPE_0 R.N.G., just do the old linear
3520Sstevel@tonic-gate  * congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
3530Sstevel@tonic-gate  * same in all ther other cases due to all the global variables that have been
3540Sstevel@tonic-gate  * set up.  The basic operation is to add the number at the rear pointer into
3550Sstevel@tonic-gate  * the one at the front pointer.  Then both pointers are advanced to the next
3560Sstevel@tonic-gate  * location cyclically in the table.  The value returned is the sum generated,
3570Sstevel@tonic-gate  * reduced to 31 bits by throwing away the "least random" low bit.
3580Sstevel@tonic-gate  * Note: the code takes advantage of the fact that both the front and
3590Sstevel@tonic-gate  * rear pointers can't wrap on the same call by not testing the rear
3600Sstevel@tonic-gate  * pointer if the front one has wrapped.
3610Sstevel@tonic-gate  * Returns a 31-bit random number.
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate long
random(void)365722Smuffin random(void)
3660Sstevel@tonic-gate {
367722Smuffin 	struct _randomjunk *rp = _randomjunk();
3680Sstevel@tonic-gate 	long		i;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (rp == 0)
3710Sstevel@tonic-gate 		return (0);
3720Sstevel@tonic-gate 	if (rp->rand_type  ==  TYPE_0)  {
3730Sstevel@tonic-gate 	    i = rp->state[0] = (rp->state[0]*1103515245 + 12345)&0x7fffffff;
3740Sstevel@tonic-gate 	} else  {
3750Sstevel@tonic-gate 	    *rp->fptr += *rp->rptr;
3760Sstevel@tonic-gate 	    i = (*rp->fptr >> 1)&0x7fffffff;	/* chucking least random bit */
3770Sstevel@tonic-gate 	    if (++rp->fptr  >=  rp->end_ptr)  {
3780Sstevel@tonic-gate 		rp->fptr = rp->state;
3790Sstevel@tonic-gate 		++rp->rptr;
3800Sstevel@tonic-gate 	    } else if (++rp->rptr  >=  rp->end_ptr)
3810Sstevel@tonic-gate 		rp->rptr = rp->state;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 	return (i);
3840Sstevel@tonic-gate }
385