11573Srgrimes/*
21573Srgrimes * Copyright (c) 1983, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
13251672Semaste * 3. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3123662Speterstatic char sccsid[] = "@(#)random.c	8.2 (Berkeley) 5/19/95";
321573Srgrimes#endif /* LIBC_SCCS and not lint */
3392986Sobrien#include <sys/cdefs.h>
3492986Sobrien__FBSDID("$FreeBSD: releng/11.0/lib/libc/stdlib/random.c 300965 2016-05-29 16:39:28Z ache $");
351573Srgrimes
3671579Sdeischen#include "namespace.h"
37249035Sdelphij#include <sys/param.h>
38249035Sdelphij#include <sys/sysctl.h>
39124738Sdas#include <stdint.h>
401573Srgrimes#include <stdlib.h>
4171579Sdeischen#include "un-namespace.h"
421573Srgrimes
431573Srgrimes/*
441573Srgrimes * random.c:
451573Srgrimes *
461573Srgrimes * An improved random number generation package.  In addition to the standard
471573Srgrimes * rand()/srand() like interface, this package also has a special state info
481573Srgrimes * interface.  The initstate() routine is called with a seed, an array of
491573Srgrimes * bytes, and a count of how many bytes are being passed in; this array is
501573Srgrimes * then initialized to contain information for random number generation with
511573Srgrimes * that much state information.  Good sizes for the amount of state
521573Srgrimes * information are 32, 64, 128, and 256 bytes.  The state can be switched by
531573Srgrimes * calling the setstate() routine with the same array as was initiallized
541573Srgrimes * with initstate().  By default, the package runs with 128 bytes of state
551573Srgrimes * information and generates far better random numbers than a linear
561573Srgrimes * congruential generator.  If the amount of state information is less than
571573Srgrimes * 32 bytes, a simple linear congruential R.N.G. is used.
581573Srgrimes *
59124738Sdas * Internally, the state information is treated as an array of uint32_t's; the
601573Srgrimes * zeroeth element of the array is the type of R.N.G. being used (small
611573Srgrimes * integer); the remainder of the array is the state information for the
62124738Sdas * R.N.G.  Thus, 32 bytes of state information will give 7 ints worth of
631573Srgrimes * state information, which will allow a degree seven polynomial.  (Note:
641573Srgrimes * the zeroeth word of state information also has some other information
651573Srgrimes * stored in it -- see setstate() for details).
668870Srgrimes *
671573Srgrimes * The random number generation technique is a linear feedback shift register
681573Srgrimes * approach, employing trinomials (since there are fewer terms to sum up that
691573Srgrimes * way).  In this approach, the least significant bit of all the numbers in
701573Srgrimes * the state table will act as a linear feedback shift register, and will
711573Srgrimes * have period 2^deg - 1 (where deg is the degree of the polynomial being
721573Srgrimes * used, assuming that the polynomial is irreducible and primitive).  The
731573Srgrimes * higher order bits will have longer periods, since their values are also
741573Srgrimes * influenced by pseudo-random carries out of the lower bits.  The total
751573Srgrimes * period of the generator is approximately deg*(2**deg - 1); thus doubling
761573Srgrimes * the amount of state information has a vast influence on the period of the
771573Srgrimes * generator.  Note: the deg*(2**deg - 1) is an approximation only good for
7892889Sobrien * large deg, when the period of the shift is the dominant factor.
791573Srgrimes * With deg equal to seven, the period is actually much longer than the
801573Srgrimes * 7*(2**7 - 1) predicted by this formula.
8123662Speter *
8223662Speter * Modified 28 December 1994 by Jacob S. Rosenberg.
8323662Speter * The following changes have been made:
8423662Speter * All references to the type u_int have been changed to unsigned long.
8523662Speter * All references to type int have been changed to type long.  Other
8623662Speter * cleanups have been made as well.  A warning for both initstate and
8723662Speter * setstate has been inserted to the effect that on Sparc platforms
8823662Speter * the 'arg_state' variable must be forced to begin on word boundaries.
8923662Speter * This can be easily done by casting a long integer array to char *.
9023662Speter * The overall logic has been left STRICTLY alone.  This software was
9123662Speter * tested on both a VAX and Sun SpacsStation with exactly the same
9223662Speter * results.  The new version and the original give IDENTICAL results.
9323662Speter * The new version is somewhat faster than the original.  As the
9423662Speter * documentation says:  "By default, the package runs with 128 bytes of
9523662Speter * state information and generates far better random numbers than a linear
9623662Speter * congruential generator.  If the amount of state information is less than
9723662Speter * 32 bytes, a simple linear congruential R.N.G. is used."  For a buffer of
9823662Speter * 128 bytes, this new version runs about 19 percent faster and for a 16
9923662Speter * byte buffer it is about 5 percent faster.
1001573Srgrimes */
1011573Srgrimes
1021573Srgrimes/*
1031573Srgrimes * For each of the currently supported random number generators, we have a
1041573Srgrimes * break value on the amount of state information (you need at least this
1051573Srgrimes * many bytes of state info to support this random number generator), a degree
1061573Srgrimes * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
1071573Srgrimes * the separation between the two lower order coefficients of the trinomial.
1081573Srgrimes */
1091573Srgrimes#define	TYPE_0		0		/* linear congruential */
1101573Srgrimes#define	BREAK_0		8
1111573Srgrimes#define	DEG_0		0
1121573Srgrimes#define	SEP_0		0
1131573Srgrimes
1141573Srgrimes#define	TYPE_1		1		/* x**7 + x**3 + 1 */
1151573Srgrimes#define	BREAK_1		32
1161573Srgrimes#define	DEG_1		7
1171573Srgrimes#define	SEP_1		3
1181573Srgrimes
1191573Srgrimes#define	TYPE_2		2		/* x**15 + x + 1 */
1201573Srgrimes#define	BREAK_2		64
1211573Srgrimes#define	DEG_2		15
1221573Srgrimes#define	SEP_2		1
1231573Srgrimes
1241573Srgrimes#define	TYPE_3		3		/* x**31 + x**3 + 1 */
1251573Srgrimes#define	BREAK_3		128
1261573Srgrimes#define	DEG_3		31
1271573Srgrimes#define	SEP_3		3
1281573Srgrimes
1291573Srgrimes#define	TYPE_4		4		/* x**63 + x + 1 */
1301573Srgrimes#define	BREAK_4		256
1311573Srgrimes#define	DEG_4		63
1321573Srgrimes#define	SEP_4		1
1331573Srgrimes
1341573Srgrimes/*
1351573Srgrimes * Array versions of the above information to make code run faster --
1361573Srgrimes * relies on fact that TYPE_i == i.
1371573Srgrimes */
1381573Srgrimes#define	MAX_TYPES	5		/* max number of types above */
1391573Srgrimes
140118731Sache#define NSHUFF 50       /* to drop some "seed -> 1st value" linearity */
141110321Sache
142124738Sdasstatic const int degrees[MAX_TYPES] =	{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
143124738Sdasstatic const int seps [MAX_TYPES] =	{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
1441573Srgrimes
1451573Srgrimes/*
1461573Srgrimes * Initially, everything is set up as if from:
1471573Srgrimes *
14818832Sache *	initstate(1, randtbl, 128);
1491573Srgrimes *
1501573Srgrimes * Note that this initialization takes advantage of the fact that srandom()
1511573Srgrimes * advances the front and rear pointers 10*rand_deg times, and hence the
1521573Srgrimes * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
1531573Srgrimes * element of the state information, which contains info about the current
1541573Srgrimes * position of the rear pointer is just
1551573Srgrimes *
1561573Srgrimes *	MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3.
1571573Srgrimes */
1581573Srgrimes
159124738Sdasstatic uint32_t randtbl[DEG_3 + 1] = {
1601573Srgrimes	TYPE_3,
161300953Sache	0x2cf41758, 0x27bb3711, 0x4916d4d1, 0x7b02f59f, 0x9b8e28eb, 0xc0e80269,
162300953Sache	0x696f5c16, 0x878f1ff5, 0x52d9c07f, 0x916a06cd, 0xb50b3a20, 0x2776970a,
163300953Sache	0xee4eb2a6, 0xe94640ec, 0xb1d65612, 0x9d1ed968, 0x1043f6b7, 0xa3432a76,
164300953Sache	0x17eacbb9, 0x3c09e2eb, 0x4f8c2b3,  0x708a1f57, 0xee341814, 0x95d0e4d2,
165300953Sache	0xb06f216c, 0x8bd2e72e, 0x8f7c38d7, 0xcfc6a8fc, 0x2a59495,  0xa20d2a69,
166300953Sache	0xe29d12d1
1671573Srgrimes};
1681573Srgrimes
1691573Srgrimes/*
1701573Srgrimes * fptr and rptr are two pointers into the state info, a front and a rear
1711573Srgrimes * pointer.  These two pointers are always rand_sep places aparts, as they
1721573Srgrimes * cycle cyclically through the state information.  (Yes, this does mean we
1731573Srgrimes * could get away with just one pointer, but the code for random() is more
1741573Srgrimes * efficient this way).  The pointers are left positioned as they would be
1751573Srgrimes * from the call
1761573Srgrimes *
1771573Srgrimes *	initstate(1, randtbl, 128);
1781573Srgrimes *
1791573Srgrimes * (The position of the rear pointer, rptr, is really 0 (as explained above
1801573Srgrimes * in the initialization of randtbl) because the state table pointer is set
1811573Srgrimes * to point to randtbl[1] (as explained below).
1821573Srgrimes */
183124738Sdasstatic uint32_t *fptr = &randtbl[SEP_3 + 1];
184124738Sdasstatic uint32_t *rptr = &randtbl[1];
1851573Srgrimes
1861573Srgrimes/*
1871573Srgrimes * The following things are the pointer to the state information table, the
1881573Srgrimes * type of the current generator, the degree of the current polynomial being
1891573Srgrimes * used, and the separation between the two pointers.  Note that for efficiency
1901573Srgrimes * of random(), we remember the first location of the state information, not
1911573Srgrimes * the zeroeth.  Hence it is valid to access state[-1], which is used to
1921573Srgrimes * store the type of the R.N.G.  Also, we remember the last location, since
1931573Srgrimes * this is more efficient than indexing every time to find the address of
1941573Srgrimes * the last element to see if the front and rear pointers have wrapped.
1951573Srgrimes */
196124738Sdasstatic uint32_t *state = &randtbl[1];
197124738Sdasstatic int rand_type = TYPE_3;
198124738Sdasstatic int rand_deg = DEG_3;
199124738Sdasstatic int rand_sep = SEP_3;
200124738Sdasstatic uint32_t *end_ptr = &randtbl[DEG_3 + 1];
2011573Srgrimes
202241031Sdesstatic inline uint32_t
203300953Sachegood_rand(uint32_t ctx)
20418832Sache{
2051573Srgrimes/*
20618832Sache * Compute x = (7^5 * x) mod (2^31 - 1)
20718832Sache * wihout overflowing 31 bits:
20818832Sache *      (2^31 - 1) = 127773 * (7^5) + 2836
20918832Sache * From "Random number generators: good ones are hard to find",
21018832Sache * Park and Miller, Communications of the ACM, vol. 31, no. 10,
21118832Sache * October 1988, p. 1195.
21218832Sache */
213300953Sache	int32_t hi, lo, x;
21418832Sache
215300953Sache	/* Transform to [1, 0x7ffffffe] range. */
216300953Sache	x = (ctx % 0x7ffffffe) + 1;
21718832Sache	hi = x / 127773;
21818832Sache	lo = x % 127773;
21918832Sache	x = 16807 * lo - 2836 * hi;
220110280Sache	if (x < 0)
22118832Sache		x += 0x7fffffff;
222300953Sache	/* Transform to [0, 0x7ffffffd] range. */
223300953Sache	return (x - 1);
22418832Sache}
22518832Sache
22618832Sache/*
2271573Srgrimes * srandom:
2281573Srgrimes *
2291573Srgrimes * Initialize the random number generator based on the given seed.  If the
2301573Srgrimes * type is the trivial no-state-information type, just remember the seed.
2311573Srgrimes * Otherwise, initializes state[] based on the given "seed" via a linear
2321573Srgrimes * congruential generator.  Then, the pointers are set to known locations
2331573Srgrimes * that are exactly rand_sep places apart.  Lastly, it cycles the state
2341573Srgrimes * information a given number of times to get rid of any initial dependencies
2351573Srgrimes * introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
2361573Srgrimes * for default usage relies on values produced by this routine.
2371573Srgrimes */
2381573Srgrimesvoid
239241031Sdessrandom(unsigned long x)
2401573Srgrimes{
241124738Sdas	int i, lim;
2421573Srgrimes
243124738Sdas	state[0] = (uint32_t)x;
2441573Srgrimes	if (rand_type == TYPE_0)
245110321Sache		lim = NSHUFF;
2461573Srgrimes	else {
2471573Srgrimes		for (i = 1; i < rand_deg; i++)
24818832Sache			state[i] = good_rand(state[i - 1]);
2491573Srgrimes		fptr = &state[rand_sep];
2501573Srgrimes		rptr = &state[0];
251110321Sache		lim = 10 * rand_deg;
2521573Srgrimes	}
253110321Sache	for (i = 0; i < lim; i++)
254110321Sache		(void)random();
2551573Srgrimes}
2561573Srgrimes
2571573Srgrimes/*
25824151Sache * srandomdev:
25924151Sache *
26024151Sache * Many programs choose the seed value in a totally predictable manner.
261249035Sdelphij * This often causes problems.  We seed the generator using pseudo-random
262249035Sdelphij * data from the kernel.
263249035Sdelphij *
264249035Sdelphij * Note that this particular seeding procedure can generate states
265249035Sdelphij * which are impossible to reproduce by calling srandom() with any
266249035Sdelphij * value, since the succeeding terms in the state buffer are no longer
267249035Sdelphij * derived from the LC algorithm applied to a fixed seed.
26824151Sache */
26926624Sachevoid
270241031Sdessrandomdev(void)
27124151Sache{
272249035Sdelphij	int mib[2];
27324151Sache	size_t len;
27424151Sache
27524151Sache	if (rand_type == TYPE_0)
276249035Sdelphij		len = sizeof(state[0]);
27724151Sache	else
278249035Sdelphij		len = rand_deg * sizeof(state[0]);
27924151Sache
280249035Sdelphij	mib[0] = CTL_KERN;
281249035Sdelphij	mib[1] = KERN_ARND;
282249035Sdelphij	sysctl(mib, 2, state, &len, NULL, 0);
28324151Sache
28424151Sache	if (rand_type != TYPE_0) {
28524151Sache		fptr = &state[rand_sep];
28624151Sache		rptr = &state[0];
28724151Sache	}
28824151Sache}
28924151Sache
29024151Sache/*
2911573Srgrimes * initstate:
2921573Srgrimes *
2931573Srgrimes * Initialize the state information in the given array of n bytes for future
2941573Srgrimes * random number generation.  Based on the number of bytes we are given, and
2951573Srgrimes * the break values for the different R.N.G.'s, we choose the best (largest)
2961573Srgrimes * one we can and set things up for it.  srandom() is then called to
2971573Srgrimes * initialize the state information.
2988870Srgrimes *
2991573Srgrimes * Note that on return from srandom(), we set state[-1] to be the type
3001573Srgrimes * multiplexed with the current value of the rear pointer; this is so
3011573Srgrimes * successive calls to initstate() won't lose this information and will be
3021573Srgrimes * able to restart with setstate().
3038870Srgrimes *
3041573Srgrimes * Note: the first thing we do is save the current state, if any, just like
3051573Srgrimes * setstate() so that it doesn't matter when initstate is called.
3061573Srgrimes *
3071573Srgrimes * Returns a pointer to the old state.
30823662Speter *
309124738Sdas * Note: The Sparc platform requires that arg_state begin on an int
31023662Speter * word boundary; otherwise a bus error will occur. Even so, lint will
31123662Speter * complain about mis-alignment, but you should disregard these messages.
3121573Srgrimes */
3131573Srgrimeschar *
314241031Sdesinitstate(unsigned long seed, char *arg_state, long n)
3151573Srgrimes{
31692889Sobrien	char *ostate = (char *)(&state[-1]);
317124738Sdas	uint32_t *int_arg_state = (uint32_t *)arg_state;
3181573Srgrimes
319300397Sache	if (n < BREAK_0)
320300397Sache		return (NULL);
3211573Srgrimes	if (rand_type == TYPE_0)
3221573Srgrimes		state[-1] = rand_type;
3231573Srgrimes	else
3241573Srgrimes		state[-1] = MAX_TYPES * (rptr - state) + rand_type;
3251573Srgrimes	if (n < BREAK_1) {
3261573Srgrimes		rand_type = TYPE_0;
3271573Srgrimes		rand_deg = DEG_0;
3281573Srgrimes		rand_sep = SEP_0;
3291573Srgrimes	} else if (n < BREAK_2) {
3301573Srgrimes		rand_type = TYPE_1;
3311573Srgrimes		rand_deg = DEG_1;
3321573Srgrimes		rand_sep = SEP_1;
3331573Srgrimes	} else if (n < BREAK_3) {
3341573Srgrimes		rand_type = TYPE_2;
3351573Srgrimes		rand_deg = DEG_2;
3361573Srgrimes		rand_sep = SEP_2;
3371573Srgrimes	} else if (n < BREAK_4) {
3381573Srgrimes		rand_type = TYPE_3;
3391573Srgrimes		rand_deg = DEG_3;
3401573Srgrimes		rand_sep = SEP_3;
3411573Srgrimes	} else {
3421573Srgrimes		rand_type = TYPE_4;
3431573Srgrimes		rand_deg = DEG_4;
3441573Srgrimes		rand_sep = SEP_4;
3451573Srgrimes	}
346124738Sdas	state = int_arg_state + 1; /* first location */
3471573Srgrimes	end_ptr = &state[rand_deg];	/* must set end_ptr before srandom */
3481573Srgrimes	srandom(seed);
3491573Srgrimes	if (rand_type == TYPE_0)
350124738Sdas		int_arg_state[0] = rand_type;
3511573Srgrimes	else
352124738Sdas		int_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type;
353241031Sdes	return (ostate);
3541573Srgrimes}
3551573Srgrimes
3561573Srgrimes/*
3571573Srgrimes * setstate:
3581573Srgrimes *
3591573Srgrimes * Restore the state from the given state array.
3601573Srgrimes *
3611573Srgrimes * Note: it is important that we also remember the locations of the pointers
3621573Srgrimes * in the current state information, and restore the locations of the pointers
3631573Srgrimes * from the old state information.  This is done by multiplexing the pointer
3641573Srgrimes * location into the zeroeth word of the state information.
3651573Srgrimes *
3661573Srgrimes * Note that due to the order in which things are done, it is OK to call
3671573Srgrimes * setstate() with the same state as the current state.
3681573Srgrimes *
3691573Srgrimes * Returns a pointer to the old state information.
37023662Speter *
371124738Sdas * Note: The Sparc platform requires that arg_state begin on an int
37223662Speter * word boundary; otherwise a bus error will occur. Even so, lint will
37323662Speter * complain about mis-alignment, but you should disregard these messages.
3741573Srgrimes */
3751573Srgrimeschar *
376241031Sdessetstate(char *arg_state)
3771573Srgrimes{
378124738Sdas	uint32_t *new_state = (uint32_t *)arg_state;
379124738Sdas	uint32_t type = new_state[0] % MAX_TYPES;
380124738Sdas	uint32_t rear = new_state[0] / MAX_TYPES;
3811573Srgrimes	char *ostate = (char *)(&state[-1]);
3821573Srgrimes
383300953Sache	if (type != TYPE_0 && rear >= degrees[type])
384300397Sache		return (NULL);
385300397Sache	if (rand_type == TYPE_0)
386300397Sache		state[-1] = rand_type;
387300397Sache	else
388300397Sache		state[-1] = MAX_TYPES * (rptr - state) + rand_type;
389300397Sache	rand_type = type;
390300397Sache	rand_deg = degrees[type];
391300397Sache	rand_sep = seps[type];
392124738Sdas	state = new_state + 1;
3931573Srgrimes	if (rand_type != TYPE_0) {
3941573Srgrimes		rptr = &state[rear];
3951573Srgrimes		fptr = &state[(rear + rand_sep) % rand_deg];
3961573Srgrimes	}
3971573Srgrimes	end_ptr = &state[rand_deg];		/* set end_ptr too */
398241031Sdes	return (ostate);
3991573Srgrimes}
4001573Srgrimes
4011573Srgrimes/*
4021573Srgrimes * random:
4031573Srgrimes *
4041573Srgrimes * If we are using the trivial TYPE_0 R.N.G., just do the old linear
4051573Srgrimes * congruential bit.  Otherwise, we do our fancy trinomial stuff, which is
4061573Srgrimes * the same in all the other cases due to all the global variables that have
4071573Srgrimes * been set up.  The basic operation is to add the number at the rear pointer
4081573Srgrimes * into the one at the front pointer.  Then both pointers are advanced to
4091573Srgrimes * the next location cyclically in the table.  The value returned is the sum
4101573Srgrimes * generated, reduced to 31 bits by throwing away the "least random" low bit.
4111573Srgrimes *
4121573Srgrimes * Note: the code takes advantage of the fact that both the front and
4131573Srgrimes * rear pointers can't wrap on the same call by not testing the rear
4141573Srgrimes * pointer if the front one has wrapped.
4151573Srgrimes *
4161573Srgrimes * Returns a 31-bit random number.
4171573Srgrimes */
4181573Srgrimeslong
419241031Sdesrandom(void)
4201573Srgrimes{
421124738Sdas	uint32_t i;
422124738Sdas	uint32_t *f, *r;
4231573Srgrimes
42423662Speter	if (rand_type == TYPE_0) {
42523662Speter		i = state[0];
426300953Sache		state[0] = i = good_rand(i);
42723662Speter	} else {
42823662Speter		/*
42923662Speter		 * Use local variables rather than static variables for speed.
43023662Speter		 */
43123662Speter		f = fptr; r = rptr;
43223662Speter		*f += *r;
433300965Sache		i = *f >> 1;	/* chucking least random bit */
43423662Speter		if (++f >= end_ptr) {
43523662Speter			f = state;
43623662Speter			++r;
43723662Speter		}
43823662Speter		else if (++r >= end_ptr) {
43923662Speter			r = state;
44023662Speter		}
44123662Speter
44223662Speter		fptr = f; rptr = r;
4431573Srgrimes	}
444241031Sdes	return ((long)i);
4451573Srgrimes}
446