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