#include "FEATURE/uwin"12#if !_UWIN || _lib_random34void _STUB_random(){}56#else78/*9* Copyright (c) 198310* The Regents of the University of California. All rights reserved.11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20* 3. Neither the name of the University nor the names of its contributors21* may be used to endorse or promote products derived from this software22* without specific prior written permission.23*24* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND25* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE28* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL29* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS30* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT32* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY33* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF34* SUCH DAMAGE.35*/3637/*38* This is derived from the Berkeley source:39* @(#)random.c 5.5 (Berkeley) 7/6/8840* It was reworked for the GNU C Library by Roland McGrath.41*/4243#define initstate ______initstate44#define random ______random45#define setstate ______setstate46#define srandom ______srandom4748#include <errno.h>49#include <limits.h>50#include <stddef.h>51#include <stdlib.h>5253#undef initstate54#undef random55#undef setstate56#undef srandom5758#if defined(__EXPORT__)59#define extern __EXPORT__60#endif6162extern long int random();6364#define PTR char*6566/* An improved random number generation package. In addition to the standard67rand()/srand() like interface, this package also has a special state info68interface. The initstate() routine is called with a seed, an array of69bytes, and a count of how many bytes are being passed in; this array is70then initialized to contain information for random number generation with71that much state information. Good sizes for the amount of state72information are 32, 64, 128, and 256 bytes. The state can be switched by73calling the setstate() function with the same array as was initiallized74with initstate(). By default, the package runs with 128 bytes of state75information and generates far better random numbers than a linear76congruential generator. If the amount of state information is less than7732 bytes, a simple linear congruential R.N.G. is used. Internally, the78state information is treated as an array of longs; the zeroeth element of79the array is the type of R.N.G. being used (small integer); the remainder80of the array is the state information for the R.N.G. Thus, 32 bytes of81state information will give 7 longs worth of state information, which will82allow a degree seven polynomial. (Note: The zeroeth word of state83information also has some other information stored in it; see setstate84for details). The random number generation technique is a linear feedback85shift register approach, employing trinomials (since there are fewer terms86to sum up that way). In this approach, the least significant bit of all87the numbers in the state table will act as a linear feedback shift register,88and will have period 2^deg - 1 (where deg is the degree of the polynomial89being used, assuming that the polynomial is irreducible and primitive).90The higher order bits will have longer periods, since their values are91also influenced by pseudo-random carries out of the lower bits. The92total period of the generator is approximately deg*(2**deg - 1); thus93doubling the amount of state information has a vast influence on the94period of the generator. Note: The deg*(2**deg - 1) is an approximation95only good for large deg, when the period of the shift register is the96dominant factor. With deg equal to seven, the period is actually much97longer than the 7*(2**7 - 1) predicted by this formula. */9899100101/* For each of the currently supported random number generators, we have a102break value on the amount of state information (you need at least thi103bytes of state info to support this random number generator), a degree for104the polynomial (actually a trinomial) that the R.N.G. is based on, and105separation between the two lower order coefficients of the trinomial. */106107/* Linear congruential. */108#define TYPE_0 0109#define BREAK_0 8110#define DEG_0 0111#define SEP_0 0112113/* x**7 + x**3 + 1. */114#define TYPE_1 1115#define BREAK_1 32116#define DEG_1 7117#define SEP_1 3118119/* x**15 + x + 1. */120#define TYPE_2 2121#define BREAK_2 64122#define DEG_2 15123#define SEP_2 1124125/* x**31 + x**3 + 1. */126#define TYPE_3 3127#define BREAK_3 128128#define DEG_3 31129#define SEP_3 3130131/* x**63 + x + 1. */132#define TYPE_4 4133#define BREAK_4 256134#define DEG_4 63135#define SEP_4 1136137138/* Array versions of the above information to make code run faster.139Relies on fact that TYPE_i == i. */140141#define MAX_TYPES 5 /* Max number of types above. */142143static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };144static int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };145146147148/* Initially, everything is set up as if from:149initstate(1, randtbl, 128);150Note that this initialization takes advantage of the fact that srandom151advances the front and rear pointers 10*rand_deg times, and hence the152rear pointer which starts at 0 will also end up at zero; thus the zeroeth153element of the state information, which contains info about the current154position of the rear pointer is just155(MAX_TYPES * (rptr - state)) + TYPE_3 == TYPE_3. */156157static long int randtbl[DEG_3 + 1] =158{159TYPE_3,160-851904987, -43806228, -2029755270, 1390239686, -1912102820,161-485608943, 1969813258, -1590463333, -1944053249, 455935928, 508023712,162-1714531963, 1800685987, -2015299881, 654595283, -1149023258,163-1470005550, -1143256056, -1325577603, -1568001885, 1275120390,164-607508183, -205999574, -1696891592, 1492211999, -1528267240,165-952028296, -189082757, 362343714, 1424981831, 2039449641,166};167168/* FPTR and RPTR are two pointers into the state info, a front and a rear169pointer. These two pointers are always rand_sep places aparts, as they170cycle through the state information. (Yes, this does mean we could get171away with just one pointer, but the code for random is more efficient172this way). The pointers are left positioned as they would be from the call:173initstate(1, randtbl, 128);174(The position of the rear pointer, rptr, is really 0 (as explained above175in the initialization of randtbl) because the state table pointer is set176to point to randtbl[1] (as explained below).) */177178static long int *fptr = &randtbl[SEP_3 + 1];179static long int *rptr = &randtbl[1];180181182183/* The following things are the pointer to the state information table,184the type of the current generator, the degree of the current polynomial185being used, and the separation between the two pointers.186Note that for efficiency of random, we remember the first location of187the state information, not the zeroeth. Hence it is valid to access188state[-1], which is used to store the type of the R.N.G.189Also, we remember the last location, since this is more efficient than190indexing every time to find the address of the last element to see if191the front and rear pointers have wrapped. */192193static long int *state = &randtbl[1];194195static int rand_type = TYPE_3;196static int rand_deg = DEG_3;197static int rand_sep = SEP_3;198199static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])];200201/* Initialize the random number generator based on the given seed. If the202type is the trivial no-state-information type, just remember the seed.203Otherwise, initializes state[] based on the given "seed" via a linear204congruential generator. Then, the pointers are set to known locations205that are exactly rand_sep places apart. Lastly, it cycles the state206information a given number of times to get rid of any initial dependencies207introduced by the L.C.R.N.G. Note that the initialization of randtbl[]208for default usage relies on values produced by this routine. */209extern void srandom(unsigned int x)210{211state[0] = x;212if (rand_type != TYPE_0)213{214register long int i;215for (i = 1; i < rand_deg; ++i)216state[i] = (1103515145 * state[i - 1]) + 12345;217fptr = &state[rand_sep];218rptr = &state[0];219for (i = 0; i < 10 * rand_deg; ++i)220(void) random();221}222}223224/* Initialize the state information in the given array of N bytes for225future random number generation. Based on the number of bytes we226are given, and the break values for the different R.N.G.'s, we choose227the best (largest) one we can and set things up for it. srandom is228then called to initialize the state information. Note that on return229from srandom, we set state[-1] to be the type multiplexed with the current230value of the rear pointer; this is so successive calls to initstate won't231lose this information and will be able to restart with setstate.232Note: The first thing we do is save the current state, if any, just like233setstate so that it doesn't matter when initstate is called.234Returns a pointer to the old state. */235extern char* initstate(unsigned int seed, char* arg_state, size_t n)236{237PTR ostate = (PTR) &state[-1];238239if (rand_type == TYPE_0)240state[-1] = rand_type;241else242state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;243if (n < BREAK_1)244{245if (n < BREAK_0)246{247errno = EINVAL;248return NULL;249}250rand_type = TYPE_0;251rand_deg = DEG_0;252rand_sep = SEP_0;253}254else if (n < BREAK_2)255{256rand_type = TYPE_1;257rand_deg = DEG_1;258rand_sep = SEP_1;259}260else if (n < BREAK_3)261{262rand_type = TYPE_2;263rand_deg = DEG_2;264rand_sep = SEP_2;265}266else if (n < BREAK_4)267{268rand_type = TYPE_3;269rand_deg = DEG_3;270rand_sep = SEP_3;271}272else273{274rand_type = TYPE_4;275rand_deg = DEG_4;276rand_sep = SEP_4;277}278279state = &((long int *) arg_state)[1]; /* First location. */280/* Must set END_PTR before srandom. */281end_ptr = &state[rand_deg];282srandom(seed);283if (rand_type == TYPE_0)284state[-1] = rand_type;285else286state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;287288return ostate;289}290291/* Restore the state from the given state array.292Note: It is important that we also remember the locations of the pointers293in the current state information, and restore the locations of the pointers294from the old state information. This is done by multiplexing the pointer295location into the zeroeth word of the state information. Note that due296to the order in which things are done, it is OK to call setstate with the297same state as the current state298Returns a pointer to the old state information. */299extern char *setstate(const char *arg_state)300{301register long int *new_state = (long int *) arg_state;302register int type = new_state[0] % MAX_TYPES;303register int rear = new_state[0] / MAX_TYPES;304PTR ostate = (PTR) &state[-1];305306if (rand_type == TYPE_0)307state[-1] = rand_type;308else309state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;310311switch (type)312{313case TYPE_0:314case TYPE_1:315case TYPE_2:316case TYPE_3:317case TYPE_4:318rand_type = type;319rand_deg = degrees[type];320rand_sep = seps[type];321break;322default:323/* State info munged. */324errno = EINVAL;325return NULL;326}327328state = &new_state[1];329if (rand_type != TYPE_0)330{331rptr = &state[rear];332fptr = &state[(rear + rand_sep) % rand_deg];333}334/* Set end_ptr too. */335end_ptr = &state[rand_deg];336337return ostate;338}339340/* If we are using the trivial TYPE_0 R.N.G., just do the old linear341congruential bit. Otherwise, we do our fancy trinomial stuff, which is the342same in all ther other cases due to all the global variables that have been343set up. The basic operation is to add the number at the rear pointer into344the one at the front pointer. Then both pointers are advanced to the next345location cyclically in the table. The value returned is the sum generated,346reduced to 31 bits by throwing away the "least random" low bit.347Note: The code takes advantage of the fact that both the front and348rear pointers can't wrap on the same call by not testing the rear349pointer if the front one has wrapped. Returns a 31-bit random number. */350351extern long int random()352{353if (rand_type == TYPE_0)354{355state[0] = ((state[0] * 1103515245) + 12345) & LONG_MAX;356return state[0];357}358else359{360long int i;361*fptr += *rptr;362/* Chucking least random bit. */363i = (*fptr >> 1) & LONG_MAX;364++fptr;365if (fptr >= end_ptr)366{367fptr = state;368++rptr;369}370else371{372++rptr;373if (rptr >= end_ptr)374rptr = state;375}376return i;377}378}379380#endif381382383