1*da2e3ebdSchin #include "FEATURE/uwin"
2*da2e3ebdSchin
3*da2e3ebdSchin #if !_UWIN || _lib_random
4*da2e3ebdSchin
_STUB_random()5*da2e3ebdSchin void _STUB_random(){}
6*da2e3ebdSchin
7*da2e3ebdSchin #else
8*da2e3ebdSchin
9*da2e3ebdSchin /*
10*da2e3ebdSchin * Copyright (c) 1983
11*da2e3ebdSchin * The Regents of the University of California. All rights reserved.
12*da2e3ebdSchin *
13*da2e3ebdSchin * Redistribution and use in source and binary forms, with or without
14*da2e3ebdSchin * modification, are permitted provided that the following conditions
15*da2e3ebdSchin * are met:
16*da2e3ebdSchin * 1. Redistributions of source code must retain the above copyright
17*da2e3ebdSchin * notice, this list of conditions and the following disclaimer.
18*da2e3ebdSchin * 2. Redistributions in binary form must reproduce the above copyright
19*da2e3ebdSchin * notice, this list of conditions and the following disclaimer in the
20*da2e3ebdSchin * documentation and/or other materials provided with the distribution.
21*da2e3ebdSchin * 3. Neither the name of the University nor the names of its contributors
22*da2e3ebdSchin * may be used to endorse or promote products derived from this software
23*da2e3ebdSchin * without specific prior written permission.
24*da2e3ebdSchin *
25*da2e3ebdSchin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*da2e3ebdSchin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*da2e3ebdSchin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*da2e3ebdSchin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*da2e3ebdSchin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*da2e3ebdSchin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*da2e3ebdSchin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*da2e3ebdSchin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*da2e3ebdSchin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*da2e3ebdSchin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*da2e3ebdSchin * SUCH DAMAGE.
36*da2e3ebdSchin */
37*da2e3ebdSchin
38*da2e3ebdSchin /*
39*da2e3ebdSchin * This is derived from the Berkeley source:
40*da2e3ebdSchin * @(#)random.c 5.5 (Berkeley) 7/6/88
41*da2e3ebdSchin * It was reworked for the GNU C Library by Roland McGrath.
42*da2e3ebdSchin */
43*da2e3ebdSchin
44*da2e3ebdSchin #define initstate ______initstate
45*da2e3ebdSchin #define random ______random
46*da2e3ebdSchin #define setstate ______setstate
47*da2e3ebdSchin #define srandom ______srandom
48*da2e3ebdSchin
49*da2e3ebdSchin #include <errno.h>
50*da2e3ebdSchin #include <limits.h>
51*da2e3ebdSchin #include <stddef.h>
52*da2e3ebdSchin #include <stdlib.h>
53*da2e3ebdSchin
54*da2e3ebdSchin #undef initstate
55*da2e3ebdSchin #undef random
56*da2e3ebdSchin #undef setstate
57*da2e3ebdSchin #undef srandom
58*da2e3ebdSchin
59*da2e3ebdSchin #if defined(__EXPORT__)
60*da2e3ebdSchin #define extern __EXPORT__
61*da2e3ebdSchin #endif
62*da2e3ebdSchin
63*da2e3ebdSchin extern long int random();
64*da2e3ebdSchin
65*da2e3ebdSchin #define PTR char*
66*da2e3ebdSchin
67*da2e3ebdSchin /* An improved random number generation package. In addition to the standard
68*da2e3ebdSchin rand()/srand() like interface, this package also has a special state info
69*da2e3ebdSchin interface. The initstate() routine is called with a seed, an array of
70*da2e3ebdSchin bytes, and a count of how many bytes are being passed in; this array is
71*da2e3ebdSchin then initialized to contain information for random number generation with
72*da2e3ebdSchin that much state information. Good sizes for the amount of state
73*da2e3ebdSchin information are 32, 64, 128, and 256 bytes. The state can be switched by
74*da2e3ebdSchin calling the setstate() function with the same array as was initiallized
75*da2e3ebdSchin with initstate(). By default, the package runs with 128 bytes of state
76*da2e3ebdSchin information and generates far better random numbers than a linear
77*da2e3ebdSchin congruential generator. If the amount of state information is less than
78*da2e3ebdSchin 32 bytes, a simple linear congruential R.N.G. is used. Internally, the
79*da2e3ebdSchin state information is treated as an array of longs; the zeroeth element of
80*da2e3ebdSchin the array is the type of R.N.G. being used (small integer); the remainder
81*da2e3ebdSchin of the array is the state information for the R.N.G. Thus, 32 bytes of
82*da2e3ebdSchin state information will give 7 longs worth of state information, which will
83*da2e3ebdSchin allow a degree seven polynomial. (Note: The zeroeth word of state
84*da2e3ebdSchin information also has some other information stored in it; see setstate
85*da2e3ebdSchin for details). The random number generation technique is a linear feedback
86*da2e3ebdSchin shift register approach, employing trinomials (since there are fewer terms
87*da2e3ebdSchin to sum up that way). In this approach, the least significant bit of all
88*da2e3ebdSchin the numbers in the state table will act as a linear feedback shift register,
89*da2e3ebdSchin and will have period 2^deg - 1 (where deg is the degree of the polynomial
90*da2e3ebdSchin being used, assuming that the polynomial is irreducible and primitive).
91*da2e3ebdSchin The higher order bits will have longer periods, since their values are
92*da2e3ebdSchin also influenced by pseudo-random carries out of the lower bits. The
93*da2e3ebdSchin total period of the generator is approximately deg*(2**deg - 1); thus
94*da2e3ebdSchin doubling the amount of state information has a vast influence on the
95*da2e3ebdSchin period of the generator. Note: The deg*(2**deg - 1) is an approximation
96*da2e3ebdSchin only good for large deg, when the period of the shift register is the
97*da2e3ebdSchin dominant factor. With deg equal to seven, the period is actually much
98*da2e3ebdSchin longer than the 7*(2**7 - 1) predicted by this formula. */
99*da2e3ebdSchin
100*da2e3ebdSchin
101*da2e3ebdSchin
102*da2e3ebdSchin /* For each of the currently supported random number generators, we have a
103*da2e3ebdSchin break value on the amount of state information (you need at least thi
104*da2e3ebdSchin bytes of state info to support this random number generator), a degree for
105*da2e3ebdSchin the polynomial (actually a trinomial) that the R.N.G. is based on, and
106*da2e3ebdSchin separation between the two lower order coefficients of the trinomial. */
107*da2e3ebdSchin
108*da2e3ebdSchin /* Linear congruential. */
109*da2e3ebdSchin #define TYPE_0 0
110*da2e3ebdSchin #define BREAK_0 8
111*da2e3ebdSchin #define DEG_0 0
112*da2e3ebdSchin #define SEP_0 0
113*da2e3ebdSchin
114*da2e3ebdSchin /* x**7 + x**3 + 1. */
115*da2e3ebdSchin #define TYPE_1 1
116*da2e3ebdSchin #define BREAK_1 32
117*da2e3ebdSchin #define DEG_1 7
118*da2e3ebdSchin #define SEP_1 3
119*da2e3ebdSchin
120*da2e3ebdSchin /* x**15 + x + 1. */
121*da2e3ebdSchin #define TYPE_2 2
122*da2e3ebdSchin #define BREAK_2 64
123*da2e3ebdSchin #define DEG_2 15
124*da2e3ebdSchin #define SEP_2 1
125*da2e3ebdSchin
126*da2e3ebdSchin /* x**31 + x**3 + 1. */
127*da2e3ebdSchin #define TYPE_3 3
128*da2e3ebdSchin #define BREAK_3 128
129*da2e3ebdSchin #define DEG_3 31
130*da2e3ebdSchin #define SEP_3 3
131*da2e3ebdSchin
132*da2e3ebdSchin /* x**63 + x + 1. */
133*da2e3ebdSchin #define TYPE_4 4
134*da2e3ebdSchin #define BREAK_4 256
135*da2e3ebdSchin #define DEG_4 63
136*da2e3ebdSchin #define SEP_4 1
137*da2e3ebdSchin
138*da2e3ebdSchin
139*da2e3ebdSchin /* Array versions of the above information to make code run faster.
140*da2e3ebdSchin Relies on fact that TYPE_i == i. */
141*da2e3ebdSchin
142*da2e3ebdSchin #define MAX_TYPES 5 /* Max number of types above. */
143*da2e3ebdSchin
144*da2e3ebdSchin static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
145*da2e3ebdSchin static int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
146*da2e3ebdSchin
147*da2e3ebdSchin
148*da2e3ebdSchin
149*da2e3ebdSchin /* Initially, everything is set up as if from:
150*da2e3ebdSchin initstate(1, randtbl, 128);
151*da2e3ebdSchin Note that this initialization takes advantage of the fact that srandom
152*da2e3ebdSchin advances the front and rear pointers 10*rand_deg times, and hence the
153*da2e3ebdSchin rear pointer which starts at 0 will also end up at zero; thus the zeroeth
154*da2e3ebdSchin element of the state information, which contains info about the current
155*da2e3ebdSchin position of the rear pointer is just
156*da2e3ebdSchin (MAX_TYPES * (rptr - state)) + TYPE_3 == TYPE_3. */
157*da2e3ebdSchin
158*da2e3ebdSchin static long int randtbl[DEG_3 + 1] =
159*da2e3ebdSchin {
160*da2e3ebdSchin TYPE_3,
161*da2e3ebdSchin -851904987, -43806228, -2029755270, 1390239686, -1912102820,
162*da2e3ebdSchin -485608943, 1969813258, -1590463333, -1944053249, 455935928, 508023712,
163*da2e3ebdSchin -1714531963, 1800685987, -2015299881, 654595283, -1149023258,
164*da2e3ebdSchin -1470005550, -1143256056, -1325577603, -1568001885, 1275120390,
165*da2e3ebdSchin -607508183, -205999574, -1696891592, 1492211999, -1528267240,
166*da2e3ebdSchin -952028296, -189082757, 362343714, 1424981831, 2039449641,
167*da2e3ebdSchin };
168*da2e3ebdSchin
169*da2e3ebdSchin /* FPTR and RPTR are two pointers into the state info, a front and a rear
170*da2e3ebdSchin pointer. These two pointers are always rand_sep places aparts, as they
171*da2e3ebdSchin cycle through the state information. (Yes, this does mean we could get
172*da2e3ebdSchin away with just one pointer, but the code for random is more efficient
173*da2e3ebdSchin this way). The pointers are left positioned as they would be from the call:
174*da2e3ebdSchin initstate(1, randtbl, 128);
175*da2e3ebdSchin (The position of the rear pointer, rptr, is really 0 (as explained above
176*da2e3ebdSchin in the initialization of randtbl) because the state table pointer is set
177*da2e3ebdSchin to point to randtbl[1] (as explained below).) */
178*da2e3ebdSchin
179*da2e3ebdSchin static long int *fptr = &randtbl[SEP_3 + 1];
180*da2e3ebdSchin static long int *rptr = &randtbl[1];
181*da2e3ebdSchin
182*da2e3ebdSchin
183*da2e3ebdSchin
184*da2e3ebdSchin /* The following things are the pointer to the state information table,
185*da2e3ebdSchin the type of the current generator, the degree of the current polynomial
186*da2e3ebdSchin being used, and the separation between the two pointers.
187*da2e3ebdSchin Note that for efficiency of random, we remember the first location of
188*da2e3ebdSchin the state information, not the zeroeth. Hence it is valid to access
189*da2e3ebdSchin state[-1], which is used to store the type of the R.N.G.
190*da2e3ebdSchin Also, we remember the last location, since this is more efficient than
191*da2e3ebdSchin indexing every time to find the address of the last element to see if
192*da2e3ebdSchin the front and rear pointers have wrapped. */
193*da2e3ebdSchin
194*da2e3ebdSchin static long int *state = &randtbl[1];
195*da2e3ebdSchin
196*da2e3ebdSchin static int rand_type = TYPE_3;
197*da2e3ebdSchin static int rand_deg = DEG_3;
198*da2e3ebdSchin static int rand_sep = SEP_3;
199*da2e3ebdSchin
200*da2e3ebdSchin static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])];
201*da2e3ebdSchin
202*da2e3ebdSchin /* Initialize the random number generator based on the given seed. If the
203*da2e3ebdSchin type is the trivial no-state-information type, just remember the seed.
204*da2e3ebdSchin Otherwise, initializes state[] based on the given "seed" via a linear
205*da2e3ebdSchin congruential generator. Then, the pointers are set to known locations
206*da2e3ebdSchin that are exactly rand_sep places apart. Lastly, it cycles the state
207*da2e3ebdSchin information a given number of times to get rid of any initial dependencies
208*da2e3ebdSchin introduced by the L.C.R.N.G. Note that the initialization of randtbl[]
209*da2e3ebdSchin for default usage relies on values produced by this routine. */
srandom(unsigned int x)210*da2e3ebdSchin extern void srandom(unsigned int x)
211*da2e3ebdSchin {
212*da2e3ebdSchin state[0] = x;
213*da2e3ebdSchin if (rand_type != TYPE_0)
214*da2e3ebdSchin {
215*da2e3ebdSchin register long int i;
216*da2e3ebdSchin for (i = 1; i < rand_deg; ++i)
217*da2e3ebdSchin state[i] = (1103515145 * state[i - 1]) + 12345;
218*da2e3ebdSchin fptr = &state[rand_sep];
219*da2e3ebdSchin rptr = &state[0];
220*da2e3ebdSchin for (i = 0; i < 10 * rand_deg; ++i)
221*da2e3ebdSchin (void) random();
222*da2e3ebdSchin }
223*da2e3ebdSchin }
224*da2e3ebdSchin
225*da2e3ebdSchin /* Initialize the state information in the given array of N bytes for
226*da2e3ebdSchin future random number generation. Based on the number of bytes we
227*da2e3ebdSchin are given, and the break values for the different R.N.G.'s, we choose
228*da2e3ebdSchin the best (largest) one we can and set things up for it. srandom is
229*da2e3ebdSchin then called to initialize the state information. Note that on return
230*da2e3ebdSchin from srandom, we set state[-1] to be the type multiplexed with the current
231*da2e3ebdSchin value of the rear pointer; this is so successive calls to initstate won't
232*da2e3ebdSchin lose this information and will be able to restart with setstate.
233*da2e3ebdSchin Note: The first thing we do is save the current state, if any, just like
234*da2e3ebdSchin setstate so that it doesn't matter when initstate is called.
235*da2e3ebdSchin Returns a pointer to the old state. */
initstate(unsigned int seed,char * arg_state,size_t n)236*da2e3ebdSchin extern char* initstate(unsigned int seed, char* arg_state, size_t n)
237*da2e3ebdSchin {
238*da2e3ebdSchin PTR ostate = (PTR) &state[-1];
239*da2e3ebdSchin
240*da2e3ebdSchin if (rand_type == TYPE_0)
241*da2e3ebdSchin state[-1] = rand_type;
242*da2e3ebdSchin else
243*da2e3ebdSchin state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
244*da2e3ebdSchin if (n < BREAK_1)
245*da2e3ebdSchin {
246*da2e3ebdSchin if (n < BREAK_0)
247*da2e3ebdSchin {
248*da2e3ebdSchin errno = EINVAL;
249*da2e3ebdSchin return NULL;
250*da2e3ebdSchin }
251*da2e3ebdSchin rand_type = TYPE_0;
252*da2e3ebdSchin rand_deg = DEG_0;
253*da2e3ebdSchin rand_sep = SEP_0;
254*da2e3ebdSchin }
255*da2e3ebdSchin else if (n < BREAK_2)
256*da2e3ebdSchin {
257*da2e3ebdSchin rand_type = TYPE_1;
258*da2e3ebdSchin rand_deg = DEG_1;
259*da2e3ebdSchin rand_sep = SEP_1;
260*da2e3ebdSchin }
261*da2e3ebdSchin else if (n < BREAK_3)
262*da2e3ebdSchin {
263*da2e3ebdSchin rand_type = TYPE_2;
264*da2e3ebdSchin rand_deg = DEG_2;
265*da2e3ebdSchin rand_sep = SEP_2;
266*da2e3ebdSchin }
267*da2e3ebdSchin else if (n < BREAK_4)
268*da2e3ebdSchin {
269*da2e3ebdSchin rand_type = TYPE_3;
270*da2e3ebdSchin rand_deg = DEG_3;
271*da2e3ebdSchin rand_sep = SEP_3;
272*da2e3ebdSchin }
273*da2e3ebdSchin else
274*da2e3ebdSchin {
275*da2e3ebdSchin rand_type = TYPE_4;
276*da2e3ebdSchin rand_deg = DEG_4;
277*da2e3ebdSchin rand_sep = SEP_4;
278*da2e3ebdSchin }
279*da2e3ebdSchin
280*da2e3ebdSchin state = &((long int *) arg_state)[1]; /* First location. */
281*da2e3ebdSchin /* Must set END_PTR before srandom. */
282*da2e3ebdSchin end_ptr = &state[rand_deg];
283*da2e3ebdSchin srandom(seed);
284*da2e3ebdSchin if (rand_type == TYPE_0)
285*da2e3ebdSchin state[-1] = rand_type;
286*da2e3ebdSchin else
287*da2e3ebdSchin state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
288*da2e3ebdSchin
289*da2e3ebdSchin return ostate;
290*da2e3ebdSchin }
291*da2e3ebdSchin
292*da2e3ebdSchin /* Restore the state from the given state array.
293*da2e3ebdSchin Note: It is important that we also remember the locations of the pointers
294*da2e3ebdSchin in the current state information, and restore the locations of the pointers
295*da2e3ebdSchin from the old state information. This is done by multiplexing the pointer
296*da2e3ebdSchin location into the zeroeth word of the state information. Note that due
297*da2e3ebdSchin to the order in which things are done, it is OK to call setstate with the
298*da2e3ebdSchin same state as the current state
299*da2e3ebdSchin Returns a pointer to the old state information. */
setstate(const char * arg_state)300*da2e3ebdSchin extern char *setstate(const char *arg_state)
301*da2e3ebdSchin {
302*da2e3ebdSchin register long int *new_state = (long int *) arg_state;
303*da2e3ebdSchin register int type = new_state[0] % MAX_TYPES;
304*da2e3ebdSchin register int rear = new_state[0] / MAX_TYPES;
305*da2e3ebdSchin PTR ostate = (PTR) &state[-1];
306*da2e3ebdSchin
307*da2e3ebdSchin if (rand_type == TYPE_0)
308*da2e3ebdSchin state[-1] = rand_type;
309*da2e3ebdSchin else
310*da2e3ebdSchin state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
311*da2e3ebdSchin
312*da2e3ebdSchin switch (type)
313*da2e3ebdSchin {
314*da2e3ebdSchin case TYPE_0:
315*da2e3ebdSchin case TYPE_1:
316*da2e3ebdSchin case TYPE_2:
317*da2e3ebdSchin case TYPE_3:
318*da2e3ebdSchin case TYPE_4:
319*da2e3ebdSchin rand_type = type;
320*da2e3ebdSchin rand_deg = degrees[type];
321*da2e3ebdSchin rand_sep = seps[type];
322*da2e3ebdSchin break;
323*da2e3ebdSchin default:
324*da2e3ebdSchin /* State info munged. */
325*da2e3ebdSchin errno = EINVAL;
326*da2e3ebdSchin return NULL;
327*da2e3ebdSchin }
328*da2e3ebdSchin
329*da2e3ebdSchin state = &new_state[1];
330*da2e3ebdSchin if (rand_type != TYPE_0)
331*da2e3ebdSchin {
332*da2e3ebdSchin rptr = &state[rear];
333*da2e3ebdSchin fptr = &state[(rear + rand_sep) % rand_deg];
334*da2e3ebdSchin }
335*da2e3ebdSchin /* Set end_ptr too. */
336*da2e3ebdSchin end_ptr = &state[rand_deg];
337*da2e3ebdSchin
338*da2e3ebdSchin return ostate;
339*da2e3ebdSchin }
340*da2e3ebdSchin
341*da2e3ebdSchin /* If we are using the trivial TYPE_0 R.N.G., just do the old linear
342*da2e3ebdSchin congruential bit. Otherwise, we do our fancy trinomial stuff, which is the
343*da2e3ebdSchin same in all ther other cases due to all the global variables that have been
344*da2e3ebdSchin set up. The basic operation is to add the number at the rear pointer into
345*da2e3ebdSchin the one at the front pointer. Then both pointers are advanced to the next
346*da2e3ebdSchin location cyclically in the table. The value returned is the sum generated,
347*da2e3ebdSchin reduced to 31 bits by throwing away the "least random" low bit.
348*da2e3ebdSchin Note: The code takes advantage of the fact that both the front and
349*da2e3ebdSchin rear pointers can't wrap on the same call by not testing the rear
350*da2e3ebdSchin pointer if the front one has wrapped. Returns a 31-bit random number. */
351*da2e3ebdSchin
random()352*da2e3ebdSchin extern long int random()
353*da2e3ebdSchin {
354*da2e3ebdSchin if (rand_type == TYPE_0)
355*da2e3ebdSchin {
356*da2e3ebdSchin state[0] = ((state[0] * 1103515245) + 12345) & LONG_MAX;
357*da2e3ebdSchin return state[0];
358*da2e3ebdSchin }
359*da2e3ebdSchin else
360*da2e3ebdSchin {
361*da2e3ebdSchin long int i;
362*da2e3ebdSchin *fptr += *rptr;
363*da2e3ebdSchin /* Chucking least random bit. */
364*da2e3ebdSchin i = (*fptr >> 1) & LONG_MAX;
365*da2e3ebdSchin ++fptr;
366*da2e3ebdSchin if (fptr >= end_ptr)
367*da2e3ebdSchin {
368*da2e3ebdSchin fptr = state;
369*da2e3ebdSchin ++rptr;
370*da2e3ebdSchin }
371*da2e3ebdSchin else
372*da2e3ebdSchin {
373*da2e3ebdSchin ++rptr;
374*da2e3ebdSchin if (rptr >= end_ptr)
375*da2e3ebdSchin rptr = state;
376*da2e3ebdSchin }
377*da2e3ebdSchin return i;
378*da2e3ebdSchin }
379*da2e3ebdSchin }
380*da2e3ebdSchin
381*da2e3ebdSchin #endif
382