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