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