os/ossrv/genericopenlibs/openenvcore/libc/src/random.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/random.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,509 @@
     1.4 +/*
     1.5 + * Copyright (c) 1983, 1993
     1.6 + *	The Regents of the University of California.  All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + * 4. Neither the name of the University nor the names of its contributors
    1.17 + *    may be used to endorse or promote products derived from this software
    1.18 + *    without specific prior written permission.
    1.19 + *
    1.20 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    1.21 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.22 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.23 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    1.24 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.25 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.26 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.27 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.28 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.29 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.30 + * SUCH DAMAGE.
    1.31 + * © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.32 + */
    1.33 +
    1.34 +#if defined(LIBC_SCCS) && !defined(lint)
    1.35 +static char sccsid[] = "@(#)random.c	8.2 (Berkeley) 5/19/95";
    1.36 +#endif /* LIBC_SCCS and not lint */
    1.37 +#include <sys/cdefs.h>
    1.38 +__FBSDID("$FreeBSD: src/lib/libc/stdlib/random.c,v 1.24 2004/01/20 03:02:18 das Exp $");
    1.39 +
    1.40 +#include <sys/time.h>          /* for srandomdev() */
    1.41 +#include <fcntl.h>             /* for srandomdev() */
    1.42 +#include <stdint.h>
    1.43 +#include <stdio.h>
    1.44 +#include <stdlib.h>
    1.45 +#include <unistd.h>            /* for srandomdev() */
    1.46 +
    1.47 +/*
    1.48 + * random.c:
    1.49 + *
    1.50 + * An improved random number generation package.  In addition to the standard
    1.51 + * rand()/srand() like interface, this package also has a special state info
    1.52 + * interface.  The initstate() routine is called with a seed, an array of
    1.53 + * bytes, and a count of how many bytes are being passed in; this array is
    1.54 + * then initialized to contain information for random number generation with
    1.55 + * that much state information.  Good sizes for the amount of state
    1.56 + * information are 32, 64, 128, and 256 bytes.  The state can be switched by
    1.57 + * calling the setstate() routine with the same array as was initiallized
    1.58 + * with initstate().  By default, the package runs with 128 bytes of state
    1.59 + * information and generates far better random numbers than a linear
    1.60 + * congruential generator.  If the amount of state information is less than
    1.61 + * 32 bytes, a simple linear congruential R.N.G. is used.
    1.62 + *
    1.63 + * Internally, the state information is treated as an array of uint32_t's; the
    1.64 + * zeroeth element of the array is the type of R.N.G. being used (small
    1.65 + * integer); the remainder of the array is the state information for the
    1.66 + * R.N.G.  Thus, 32 bytes of state information will give 7 ints worth of
    1.67 + * state information, which will allow a degree seven polynomial.  (Note:
    1.68 + * the zeroeth word of state information also has some other information
    1.69 + * stored in it -- see setstate() for details).
    1.70 + *
    1.71 + * The random number generation technique is a linear feedback shift register
    1.72 + * approach, employing trinomials (since there are fewer terms to sum up that
    1.73 + * way).  In this approach, the least significant bit of all the numbers in
    1.74 + * the state table will act as a linear feedback shift register, and will
    1.75 + * have period 2^deg - 1 (where deg is the degree of the polynomial being
    1.76 + * used, assuming that the polynomial is irreducible and primitive).  The
    1.77 + * higher order bits will have longer periods, since their values are also
    1.78 + * influenced by pseudo-random carries out of the lower bits.  The total
    1.79 + * period of the generator is approximately deg*(2**deg - 1); thus doubling
    1.80 + * the amount of state information has a vast influence on the period of the
    1.81 + * generator.  Note: the deg*(2**deg - 1) is an approximation only good for
    1.82 + * large deg, when the period of the shift is the dominant factor.
    1.83 + * With deg equal to seven, the period is actually much longer than the
    1.84 + * 7*(2**7 - 1) predicted by this formula.
    1.85 + *
    1.86 + * Modified 28 December 1994 by Jacob S. Rosenberg.
    1.87 + * The following changes have been made:
    1.88 + * All references to the type u_int have been changed to unsigned long.
    1.89 + * All references to type int have been changed to type long.  Other
    1.90 + * cleanups have been made as well.  A warning for both initstate and
    1.91 + * setstate has been inserted to the effect that on Sparc platforms
    1.92 + * the 'arg_state' variable must be forced to begin on word boundaries.
    1.93 + * This can be easily done by casting a long integer array to char *.
    1.94 + * The overall logic has been left STRICTLY alone.  This software was
    1.95 + * tested on both a VAX and Sun SpacsStation with exactly the same
    1.96 + * results.  The new version and the original give IDENTICAL results.
    1.97 + * The new version is somewhat faster than the original.  As the
    1.98 + * documentation says:  "By default, the package runs with 128 bytes of
    1.99 + * state information and generates far better random numbers than a linear
   1.100 + * congruential generator.  If the amount of state information is less than
   1.101 + * 32 bytes, a simple linear congruential R.N.G. is used."  For a buffer of
   1.102 + * 128 bytes, this new version runs about 19 percent faster and for a 16
   1.103 + * byte buffer it is about 5 percent faster.
   1.104 + */
   1.105 +
   1.106 +/*
   1.107 + * For each of the currently supported random number generators, we have a
   1.108 + * break value on the amount of state information (you need at least this
   1.109 + * many bytes of state info to support this random number generator), a degree
   1.110 + * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
   1.111 + * the separation between the two lower order coefficients of the trinomial.
   1.112 + */
   1.113 +#define	TYPE_0		0		/* linear congruential */
   1.114 +#define	BREAK_0		8
   1.115 +#define	DEG_0		0
   1.116 +#define	SEP_0		0
   1.117 +
   1.118 +#define	TYPE_1		1		/* x**7 + x**3 + 1 */
   1.119 +#define	BREAK_1		32
   1.120 +#define	DEG_1		7
   1.121 +#define	SEP_1		3
   1.122 +
   1.123 +#define	TYPE_2		2		/* x**15 + x + 1 */
   1.124 +#define	BREAK_2		64
   1.125 +#define	DEG_2		15
   1.126 +#define	SEP_2		1
   1.127 +
   1.128 +#define	TYPE_3		3		/* x**31 + x**3 + 1 */
   1.129 +#define	BREAK_3		128
   1.130 +#define	DEG_3		31
   1.131 +#define	SEP_3		3
   1.132 +
   1.133 +#define	TYPE_4		4		/* x**63 + x + 1 */
   1.134 +#define	BREAK_4		256
   1.135 +#define	DEG_4		63
   1.136 +#define	SEP_4		1
   1.137 +
   1.138 +/*
   1.139 + * Array versions of the above information to make code run faster --
   1.140 + * relies on fact that TYPE_i == i.
   1.141 + */
   1.142 +#define	MAX_TYPES	5		/* max number of types above */
   1.143 +
   1.144 +#ifdef  USE_WEAK_SEEDING
   1.145 +#define NSHUFF 0
   1.146 +#else   /* !USE_WEAK_SEEDING */
   1.147 +#define NSHUFF 50       /* to drop some "seed -> 1st value" linearity */
   1.148 +#endif  /* !USE_WEAK_SEEDING */
   1.149 +
   1.150 +static const int degrees[MAX_TYPES] =	{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
   1.151 +static const int seps [MAX_TYPES] =	{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
   1.152 +
   1.153 +/*
   1.154 + * Initially, everything is set up as if from:
   1.155 + *
   1.156 + *	initstate(1, randtbl, 128);
   1.157 + *
   1.158 + * Note that this initialization takes advantage of the fact that srandom()
   1.159 + * advances the front and rear pointers 10*rand_deg times, and hence the
   1.160 + * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
   1.161 + * element of the state information, which contains info about the current
   1.162 + * position of the rear pointer is just
   1.163 + *
   1.164 + *	MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3.
   1.165 + */
   1.166 +
   1.167 +static uint32_t randtbl[DEG_3 + 1] = {
   1.168 +	TYPE_3,
   1.169 +#ifdef  USE_WEAK_SEEDING
   1.170 +/* Historic implementation compatibility */
   1.171 +/* The random sequences do not vary much with the seed */
   1.172 +	0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5,
   1.173 +	0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
   1.174 +	0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88,
   1.175 +	0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
   1.176 +	0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b,
   1.177 +	0x27fb47b9,
   1.178 +#else   /* !USE_WEAK_SEEDING */
   1.179 +	0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05,
   1.180 +	0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454,
   1.181 +	0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471,
   1.182 +	0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1,
   1.183 +	0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41,
   1.184 +	0xf3bec5da
   1.185 +#endif  /* !USE_WEAK_SEEDING */
   1.186 +};
   1.187 +
   1.188 +/*
   1.189 + * fptr and rptr are two pointers into the state info, a front and a rear
   1.190 + * pointer.  These two pointers are always rand_sep places aparts, as they
   1.191 + * cycle cyclically through the state information.  (Yes, this does mean we
   1.192 + * could get away with just one pointer, but the code for random() is more
   1.193 + * efficient this way).  The pointers are left positioned as they would be
   1.194 + * from the call
   1.195 + *
   1.196 + *	initstate(1, randtbl, 128);
   1.197 + *
   1.198 + * (The position of the rear pointer, rptr, is really 0 (as explained above
   1.199 + * in the initialization of randtbl) because the state table pointer is set
   1.200 + * to point to randtbl[1] (as explained below).
   1.201 + */
   1.202 +static uint32_t *fptr = &randtbl[SEP_3 + 1];
   1.203 +static uint32_t *rptr = &randtbl[1];
   1.204 +
   1.205 +/*
   1.206 + * The following things are the pointer to the state information table, the
   1.207 + * type of the current generator, the degree of the current polynomial being
   1.208 + * used, and the separation between the two pointers.  Note that for efficiency
   1.209 + * of random(), we remember the first location of the state information, not
   1.210 + * the zeroeth.  Hence it is valid to access state[-1], which is used to
   1.211 + * store the type of the R.N.G.  Also, we remember the last location, since
   1.212 + * this is more efficient than indexing every time to find the address of
   1.213 + * the last element to see if the front and rear pointers have wrapped.
   1.214 + */
   1.215 +static uint32_t *state = &randtbl[1];
   1.216 +static int rand_type = TYPE_3;
   1.217 +static int rand_deg = DEG_3;
   1.218 +static int rand_sep = SEP_3;
   1.219 +static uint32_t *end_ptr = &randtbl[DEG_3 + 1];
   1.220 +
   1.221 +static inline uint32_t good_rand(int32_t);
   1.222 +
   1.223 +static inline uint32_t good_rand (x)
   1.224 +	int32_t x;
   1.225 +{
   1.226 +#ifdef  USE_WEAK_SEEDING
   1.227 +/*
   1.228 + * Historic implementation compatibility.
   1.229 + * The random sequences do not vary much with the seed,
   1.230 + * even with overflowing.
   1.231 + */
   1.232 +	return (1103515245 * x + 12345);
   1.233 +#else   /* !USE_WEAK_SEEDING */
   1.234 +/*
   1.235 + * Compute x = (7^5 * x) mod (2^31 - 1)
   1.236 + * wihout overflowing 31 bits:
   1.237 + *      (2^31 - 1) = 127773 * (7^5) + 2836
   1.238 + * From "Random number generators: good ones are hard to find",
   1.239 + * Park and Miller, Communications of the ACM, vol. 31, no. 10,
   1.240 + * October 1988, p. 1195.
   1.241 + */
   1.242 +	int32_t hi, lo;
   1.243 +
   1.244 +	/* Can't be initialized with 0, so use another value. */
   1.245 +	if (x == 0)
   1.246 +		x = 123459876;
   1.247 +	hi = x / 127773;
   1.248 +	lo = x % 127773;
   1.249 +	x = 16807 * lo - 2836 * hi;
   1.250 +	if (x < 0)
   1.251 +		x += 0x7fffffff;
   1.252 +	return (x);
   1.253 +#endif  /* !USE_WEAK_SEEDING */
   1.254 +}
   1.255 +
   1.256 +/*
   1.257 + * srandom:
   1.258 + *
   1.259 + * Initialize the random number generator based on the given seed.  If the
   1.260 + * type is the trivial no-state-information type, just remember the seed.
   1.261 + * Otherwise, initializes state[] based on the given "seed" via a linear
   1.262 + * congruential generator.  Then, the pointers are set to known locations
   1.263 + * that are exactly rand_sep places apart.  Lastly, it cycles the state
   1.264 + * information a given number of times to get rid of any initial dependencies
   1.265 + * introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
   1.266 + * for default usage relies on values produced by this routine.
   1.267 + */
   1.268 +
   1.269 +EXPORT_C
   1.270 +void
   1.271 +srandom(x)
   1.272 +	unsigned long x;
   1.273 +{
   1.274 +	int i, lim;
   1.275 +
   1.276 +	state[0] = (uint32_t)x;
   1.277 +	if (rand_type == TYPE_0)
   1.278 +		lim = NSHUFF;
   1.279 +	else {
   1.280 +		for (i = 1; i < rand_deg; i++)
   1.281 +			state[i] = good_rand(state[i - 1]);
   1.282 +		fptr = &state[rand_sep];
   1.283 +		rptr = &state[0];
   1.284 +		lim = 10 * rand_deg;
   1.285 +	}
   1.286 +	for (i = 0; i < lim; i++)
   1.287 +		(void)random();
   1.288 +}
   1.289 +#ifdef __SYMBIAN_COMPILE_UNUSED__
   1.290 +/*
   1.291 + * srandomdev:
   1.292 + *
   1.293 + * Many programs choose the seed value in a totally predictable manner.
   1.294 + * This often causes problems.  We seed the generator using the much more
   1.295 + * secure random(4) interface.  Note that this particular seeding
   1.296 + * procedure can generate states which are impossible to reproduce by
   1.297 + * calling srandom() with any value, since the succeeding terms in the
   1.298 + * state buffer are no longer derived from the LC algorithm applied to
   1.299 + * a fixed seed.
   1.300 + */
   1.301 +void
   1.302 +srandomdev()
   1.303 +{
   1.304 +	int fd, done;
   1.305 +	size_t len;
   1.306 +
   1.307 +	if (rand_type == TYPE_0)
   1.308 +		len = sizeof state[0];
   1.309 +	else
   1.310 +		len = rand_deg * sizeof state[0];
   1.311 +
   1.312 +	done = 0;
   1.313 +	fd = open("/dev/random", O_RDONLY, 0);
   1.314 +	if (fd >= 0) {
   1.315 +		if (read(fd, (void *) state, len) == (ssize_t) len)
   1.316 +			done = 1;
   1.317 +		close(fd);
   1.318 +	}
   1.319 +
   1.320 +	if (!done) {
   1.321 +		struct timeval tv;
   1.322 +		unsigned long junk = 0;
   1.323 +
   1.324 +		gettimeofday(&tv, NULL);
   1.325 +		srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk);
   1.326 +		return;
   1.327 +	}
   1.328 +
   1.329 +	if (rand_type != TYPE_0) {
   1.330 +		fptr = &state[rand_sep];
   1.331 +		rptr = &state[0];
   1.332 +	}
   1.333 +}
   1.334 +#endif
   1.335 +/*
   1.336 + * initstate:
   1.337 + *
   1.338 + * Initialize the state information in the given array of n bytes for future
   1.339 + * random number generation.  Based on the number of bytes we are given, and
   1.340 + * the break values for the different R.N.G.'s, we choose the best (largest)
   1.341 + * one we can and set things up for it.  srandom() is then called to
   1.342 + * initialize the state information.
   1.343 + *
   1.344 + * Note that on return from srandom(), we set state[-1] to be the type
   1.345 + * multiplexed with the current value of the rear pointer; this is so
   1.346 + * successive calls to initstate() won't lose this information and will be
   1.347 + * able to restart with setstate().
   1.348 + *
   1.349 + * Note: the first thing we do is save the current state, if any, just like
   1.350 + * setstate() so that it doesn't matter when initstate is called.
   1.351 + *
   1.352 + * Returns a pointer to the old state.
   1.353 + *
   1.354 + * Note: The Sparc platform requires that arg_state begin on an int
   1.355 + * word boundary; otherwise a bus error will occur. Even so, lint will
   1.356 + * complain about mis-alignment, but you should disregard these messages.
   1.357 + */
   1.358 +
   1.359 +EXPORT_C
   1.360 +char *
   1.361 +initstate(seed, arg_state, n)
   1.362 +	unsigned long seed;		/* seed for R.N.G. */
   1.363 +	char *arg_state;		/* pointer to state array */
   1.364 +	long n;				/* # bytes of state info */
   1.365 +{
   1.366 +	char *ostate = (char *)(&state[-1]);
   1.367 +	uint32_t *int_arg_state = (uint32_t *)arg_state;
   1.368 +
   1.369 +	if (rand_type == TYPE_0)
   1.370 +		state[-1] = rand_type;
   1.371 +	else
   1.372 +		state[-1] = MAX_TYPES * (rptr - state) + rand_type;
   1.373 +	if (n < BREAK_0) {
   1.374 +		(void)fprintf(stderr,
   1.375 +		    "random: not enough state (%ld bytes); ignored.\n", n);
   1.376 +		return(0);
   1.377 +	}
   1.378 +	if (n < BREAK_1) {
   1.379 +		rand_type = TYPE_0;
   1.380 +		rand_deg = DEG_0;
   1.381 +		rand_sep = SEP_0;
   1.382 +	} else if (n < BREAK_2) {
   1.383 +		rand_type = TYPE_1;
   1.384 +		rand_deg = DEG_1;
   1.385 +		rand_sep = SEP_1;
   1.386 +	} else if (n < BREAK_3) {
   1.387 +		rand_type = TYPE_2;
   1.388 +		rand_deg = DEG_2;
   1.389 +		rand_sep = SEP_2;
   1.390 +	} else if (n < BREAK_4) {
   1.391 +		rand_type = TYPE_3;
   1.392 +		rand_deg = DEG_3;
   1.393 +		rand_sep = SEP_3;
   1.394 +	} else {
   1.395 +		rand_type = TYPE_4;
   1.396 +		rand_deg = DEG_4;
   1.397 +		rand_sep = SEP_4;
   1.398 +	}
   1.399 +	state = int_arg_state + 1; /* first location */
   1.400 +	end_ptr = &state[rand_deg];	/* must set end_ptr before srandom */
   1.401 +	srandom(seed);
   1.402 +	if (rand_type == TYPE_0)
   1.403 +		int_arg_state[0] = rand_type;
   1.404 +	else
   1.405 +		int_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type;
   1.406 +	return(ostate);
   1.407 +}
   1.408 +
   1.409 +/*
   1.410 + * setstate:
   1.411 + *
   1.412 + * Restore the state from the given state array.
   1.413 + *
   1.414 + * Note: it is important that we also remember the locations of the pointers
   1.415 + * in the current state information, and restore the locations of the pointers
   1.416 + * from the old state information.  This is done by multiplexing the pointer
   1.417 + * location into the zeroeth word of the state information.
   1.418 + *
   1.419 + * Note that due to the order in which things are done, it is OK to call
   1.420 + * setstate() with the same state as the current state.
   1.421 + *
   1.422 + * Returns a pointer to the old state information.
   1.423 + *
   1.424 + * Note: The Sparc platform requires that arg_state begin on an int
   1.425 + * word boundary; otherwise a bus error will occur. Even so, lint will
   1.426 + * complain about mis-alignment, but you should disregard these messages.
   1.427 + */
   1.428 +
   1.429 +EXPORT_C
   1.430 +char *
   1.431 +setstate(arg_state)
   1.432 +	char *arg_state;		/* pointer to state array */
   1.433 +{
   1.434 +	uint32_t *new_state = (uint32_t *)arg_state;
   1.435 +	uint32_t type = new_state[0] % MAX_TYPES;
   1.436 +	uint32_t rear = new_state[0] / MAX_TYPES;
   1.437 +	char *ostate = (char *)(&state[-1]);
   1.438 +
   1.439 +	if (rand_type == TYPE_0)
   1.440 +		state[-1] = rand_type;
   1.441 +	else
   1.442 +		state[-1] = MAX_TYPES * (rptr - state) + rand_type;
   1.443 +	switch(type) {
   1.444 +	case TYPE_0:
   1.445 +	case TYPE_1:
   1.446 +	case TYPE_2:
   1.447 +	case TYPE_3:
   1.448 +	case TYPE_4:
   1.449 +		rand_type = type;
   1.450 +		rand_deg = degrees[type];
   1.451 +		rand_sep = seps[type];
   1.452 +		break;
   1.453 +	default:
   1.454 +		(void)fprintf(stderr,
   1.455 +		    "random: state info corrupted; not changed.\n");
   1.456 +	}
   1.457 +	state = new_state + 1;
   1.458 +	if (rand_type != TYPE_0) {
   1.459 +		rptr = &state[rear];
   1.460 +		fptr = &state[(rear + rand_sep) % rand_deg];
   1.461 +	}
   1.462 +	end_ptr = &state[rand_deg];		/* set end_ptr too */
   1.463 +	return(ostate);
   1.464 +}
   1.465 +
   1.466 +/*
   1.467 + * random:
   1.468 + *
   1.469 + * If we are using the trivial TYPE_0 R.N.G., just do the old linear
   1.470 + * congruential bit.  Otherwise, we do our fancy trinomial stuff, which is
   1.471 + * the same in all the other cases due to all the global variables that have
   1.472 + * been set up.  The basic operation is to add the number at the rear pointer
   1.473 + * into the one at the front pointer.  Then both pointers are advanced to
   1.474 + * the next location cyclically in the table.  The value returned is the sum
   1.475 + * generated, reduced to 31 bits by throwing away the "least random" low bit.
   1.476 + *
   1.477 + * Note: the code takes advantage of the fact that both the front and
   1.478 + * rear pointers can't wrap on the same call by not testing the rear
   1.479 + * pointer if the front one has wrapped.
   1.480 + *
   1.481 + * Returns a 31-bit random number.
   1.482 + */
   1.483 +
   1.484 +EXPORT_C
   1.485 +long
   1.486 +random()
   1.487 +{
   1.488 +	uint32_t i;
   1.489 +	uint32_t *f, *r;
   1.490 +
   1.491 +	if (rand_type == TYPE_0) {
   1.492 +		i = state[0];
   1.493 +		state[0] = i = (good_rand(i)) & 0x7fffffff;
   1.494 +	} else {
   1.495 +		/*
   1.496 +		 * Use local variables rather than static variables for speed.
   1.497 +		 */
   1.498 +		f = fptr; r = rptr;
   1.499 +		*f += *r;
   1.500 +		i = (*f >> 1) & 0x7fffffff;	/* chucking least random bit */
   1.501 +		if (++f >= end_ptr) {
   1.502 +			f = state;
   1.503 +			++r;
   1.504 +		}
   1.505 +		else if (++r >= end_ptr) {
   1.506 +			r = state;
   1.507 +		}
   1.508 +
   1.509 +		fptr = f; rptr = r;
   1.510 +	}
   1.511 +	return((long)i);
   1.512 +}