xref: /netbsd-src/external/bsd/ntp/dist/libntp/ntp_random.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: ntp_random.c,v 1.6 2024/08/18 20:47:13 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4abb0f93cSkardel  * Copyright (c) 1983, 1993
5abb0f93cSkardel  *	The Regents of the University of California.  All rights reserved.
6abb0f93cSkardel  *
7abb0f93cSkardel  * Redistribution and use in source and binary forms, with or without
8abb0f93cSkardel  * modification, are permitted provided that the following conditions
9abb0f93cSkardel  * are met:
10abb0f93cSkardel  * 1. Redistributions of source code must retain the above copyright
11abb0f93cSkardel  *    notice, this list of conditions and the following disclaimer.
12abb0f93cSkardel  * 2. Redistributions in binary form must reproduce the above copyright
13abb0f93cSkardel  *    notice, this list of conditions and the following disclaimer in the
14abb0f93cSkardel  *    documentation and/or other materials provided with the distribution.
15abb0f93cSkardel  * 3. All advertising materials mentioning features or use of this software
16abb0f93cSkardel  *    must display the following acknowledgement:
17abb0f93cSkardel  *	This product includes software developed by the University of
18abb0f93cSkardel  *	California, Berkeley and its contributors.
19abb0f93cSkardel  * 4. Neither the name of the University nor the names of its contributors
20abb0f93cSkardel  *    may be used to endorse or promote products derived from this software
21abb0f93cSkardel  *    without specific prior written permission.
22abb0f93cSkardel  *
23abb0f93cSkardel  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24abb0f93cSkardel  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25abb0f93cSkardel  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26abb0f93cSkardel  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27abb0f93cSkardel  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28abb0f93cSkardel  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29abb0f93cSkardel  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30abb0f93cSkardel  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31abb0f93cSkardel  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32abb0f93cSkardel  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33abb0f93cSkardel  * SUCH DAMAGE.
34abb0f93cSkardel  *
35abb0f93cSkardel  * $FreeBSD: src/lib/libc/stdlib/random.c,v 1.4.2.2 1999/09/05 11:16:45 peter Exp $
36abb0f93cSkardel  *
37abb0f93cSkardel  */
38abb0f93cSkardel 
39abb0f93cSkardel #if defined(LIBC_SCCS) && !defined(lint)
40abb0f93cSkardel static char sccsid[] = "@(#)random.c	8.2 (Berkeley) 5/19/95";
41abb0f93cSkardel #endif /* LIBC_SCCS and not lint */
42abb0f93cSkardel 
43abb0f93cSkardel #include "config.h"
44abb0f93cSkardel #include <sys/types.h>
45abb0f93cSkardel #ifdef HAVE_UNISTD_H
46abb0f93cSkardel # include <unistd.h>
47abb0f93cSkardel #endif
48abb0f93cSkardel #include <stdio.h>
49abb0f93cSkardel 
508585484eSchristos #include <l_stdlib.h>
51abb0f93cSkardel #include <ntp_random.h>
52abb0f93cSkardel #include <ntp_unixtime.h>
53abb0f93cSkardel 
54abb0f93cSkardel /*
55abb0f93cSkardel  * random.c:
56abb0f93cSkardel  *
57abb0f93cSkardel  * An improved random number generation package.  In addition to the standard
58abb0f93cSkardel  * rand()/srand() like interface, this package also has a special state info
59abb0f93cSkardel  * interface.  The initstate() routine is called with a seed, an array of
60abb0f93cSkardel  * bytes, and a count of how many bytes are being passed in; this array is
61abb0f93cSkardel  * then initialized to contain information for random number generation with
62abb0f93cSkardel  * that much state information.  Good sizes for the amount of state
63abb0f93cSkardel  * information are 32, 64, 128, and 256 bytes.  The state can be switched by
64abb0f93cSkardel  * calling the setstate() routine with the same array as was initiallized
65abb0f93cSkardel  * with initstate().  By default, the package runs with 128 bytes of state
66abb0f93cSkardel  * information and generates far better random numbers than a linear
67abb0f93cSkardel  * congruential generator.  If the amount of state information is less than
68abb0f93cSkardel  * 32 bytes, a simple linear congruential R.N.G. is used.
69abb0f93cSkardel  *
70abb0f93cSkardel  * Internally, the state information is treated as an array of longs; the
71abb0f93cSkardel  * zeroeth element of the array is the type of R.N.G. being used (small
72abb0f93cSkardel  * integer); the remainder of the array is the state information for the
73abb0f93cSkardel  * R.N.G.  Thus, 32 bytes of state information will give 7 longs worth of
74abb0f93cSkardel  * state information, which will allow a degree seven polynomial.  (Note:
75abb0f93cSkardel  * the zeroeth word of state information also has some other information
76abb0f93cSkardel  * stored in it -- see setstate() for details).
77abb0f93cSkardel  *
78abb0f93cSkardel  * The random number generation technique is a linear feedback shift register
79abb0f93cSkardel  * approach, employing trinomials (since there are fewer terms to sum up that
80abb0f93cSkardel  * way).  In this approach, the least significant bit of all the numbers in
81abb0f93cSkardel  * the state table will act as a linear feedback shift register, and will
82abb0f93cSkardel  * have period 2^deg - 1 (where deg is the degree of the polynomial being
83abb0f93cSkardel  * used, assuming that the polynomial is irreducible and primitive).  The
84abb0f93cSkardel  * higher order bits will have longer periods, since their values are also
85abb0f93cSkardel  * influenced by pseudo-random carries out of the lower bits.  The total
86abb0f93cSkardel  * period of the generator is approximately deg*(2**deg - 1); thus doubling
87abb0f93cSkardel  * the amount of state information has a vast influence on the period of the
88abb0f93cSkardel  * generator.  Note: the deg*(2**deg - 1) is an approximation only good for
89abb0f93cSkardel  * large deg, when the period of the shift register is the dominant factor.
90abb0f93cSkardel  * With deg equal to seven, the period is actually much longer than the
91abb0f93cSkardel  * 7*(2**7 - 1) predicted by this formula.
92abb0f93cSkardel  *
93abb0f93cSkardel  * Modified 28 December 1994 by Jacob S. Rosenberg.
94abb0f93cSkardel  * The following changes have been made:
95abb0f93cSkardel  * All references to the type u_int have been changed to unsigned long.
96abb0f93cSkardel  * All references to type int have been changed to type long.  Other
97abb0f93cSkardel  * cleanups have been made as well.  A warning for both initstate and
98abb0f93cSkardel  * setstate has been inserted to the effect that on Sparc platforms
99abb0f93cSkardel  * the 'arg_state' variable must be forced to begin on word boundaries.
100abb0f93cSkardel  * This can be easily done by casting a long integer array to char *.
101abb0f93cSkardel  * The overall logic has been left STRICTLY alone.  This software was
102abb0f93cSkardel  * tested on both a VAX and Sun SpacsStation with exactly the same
103abb0f93cSkardel  * results.  The new version and the original give IDENTICAL results.
104abb0f93cSkardel  * The new version is somewhat faster than the original.  As the
105abb0f93cSkardel  * documentation says:  "By default, the package runs with 128 bytes of
106abb0f93cSkardel  * state information and generates far better random numbers than a linear
107abb0f93cSkardel  * congruential generator.  If the amount of state information is less than
108abb0f93cSkardel  * 32 bytes, a simple linear congruential R.N.G. is used."  For a buffer of
109abb0f93cSkardel  * 128 bytes, this new version runs about 19 percent faster and for a 16
110abb0f93cSkardel  * byte buffer it is about 5 percent faster.
111abb0f93cSkardel  */
112abb0f93cSkardel 
113abb0f93cSkardel /*
114abb0f93cSkardel  * For each of the currently supported random number generators, we have a
115abb0f93cSkardel  * break value on the amount of state information (you need at least this
116abb0f93cSkardel  * many bytes of state info to support this random number generator), a degree
117abb0f93cSkardel  * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
118abb0f93cSkardel  * the separation between the two lower order coefficients of the trinomial.
119abb0f93cSkardel  */
120abb0f93cSkardel #define	TYPE_0		0		/* linear congruential */
121abb0f93cSkardel #define	BREAK_0		8
122abb0f93cSkardel #define	DEG_0		0
123abb0f93cSkardel #define	SEP_0		0
124abb0f93cSkardel 
125abb0f93cSkardel #define	TYPE_1		1		/* x**7 + x**3 + 1 */
126abb0f93cSkardel #define	BREAK_1		32
127abb0f93cSkardel #define	DEG_1		7
128abb0f93cSkardel #define	SEP_1		3
129abb0f93cSkardel 
130abb0f93cSkardel #define	TYPE_2		2		/* x**15 + x + 1 */
131abb0f93cSkardel #define	BREAK_2		64
132abb0f93cSkardel #define	DEG_2		15
133abb0f93cSkardel #define	SEP_2		1
134abb0f93cSkardel 
135abb0f93cSkardel #define	TYPE_3		3		/* x**31 + x**3 + 1 */
136abb0f93cSkardel #define	BREAK_3		128
137abb0f93cSkardel #define	DEG_3		31
138abb0f93cSkardel #define	SEP_3		3
139abb0f93cSkardel 
140abb0f93cSkardel #define	TYPE_4		4		/* x**63 + x + 1 */
141abb0f93cSkardel #define	BREAK_4		256
142abb0f93cSkardel #define	DEG_4		63
143abb0f93cSkardel #define	SEP_4		1
144abb0f93cSkardel 
145abb0f93cSkardel #define	MAX_TYPES	5		/* max number of types above */
146abb0f93cSkardel 
147abb0f93cSkardel /*
148abb0f93cSkardel  * Initially, everything is set up as if from:
149abb0f93cSkardel  *
150abb0f93cSkardel  *	initstate(1, randtbl, 128);
151abb0f93cSkardel  *
152abb0f93cSkardel  * Note that this initialization takes advantage of the fact that srandom()
153abb0f93cSkardel  * advances the front and rear pointers 10*rand_deg times, and hence the
154abb0f93cSkardel  * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
155abb0f93cSkardel  * element of the state information, which contains info about the current
156abb0f93cSkardel  * position of the rear pointer is just
157abb0f93cSkardel  *
158abb0f93cSkardel  *	MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3.
159abb0f93cSkardel  */
160abb0f93cSkardel 
1618585484eSchristos static unsigned long randtbl[DEG_3 + 1] = {
162abb0f93cSkardel 	TYPE_3,
163abb0f93cSkardel #ifdef  USE_WEAK_SEEDING
164abb0f93cSkardel /* Historic implementation compatibility */
165abb0f93cSkardel /* The random sequences do not vary much with the seed */
166abb0f93cSkardel 	0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5,
167abb0f93cSkardel 	0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
168abb0f93cSkardel 	0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88,
169abb0f93cSkardel 	0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
170abb0f93cSkardel 	0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b,
171abb0f93cSkardel 	0x27fb47b9,
172abb0f93cSkardel #else   /* !USE_WEAK_SEEDING */
173abb0f93cSkardel 	0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05,
174abb0f93cSkardel 	0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454,
175abb0f93cSkardel 	0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471,
176abb0f93cSkardel 	0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1,
177abb0f93cSkardel 	0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41,
178abb0f93cSkardel 	0xf3bec5da
179abb0f93cSkardel #endif  /* !USE_WEAK_SEEDING */
180abb0f93cSkardel };
181abb0f93cSkardel 
182abb0f93cSkardel /*
183abb0f93cSkardel  * fptr and rptr are two pointers into the state info, a front and a rear
184abb0f93cSkardel  * pointer.  These two pointers are always rand_sep places aparts, as they
185abb0f93cSkardel  * cycle cyclically through the state information.  (Yes, this does mean we
186abb0f93cSkardel  * could get away with just one pointer, but the code for random() is more
187abb0f93cSkardel  * efficient this way).  The pointers are left positioned as they would be
188abb0f93cSkardel  * from the call
189abb0f93cSkardel  *
190abb0f93cSkardel  *	initstate(1, randtbl, 128);
191abb0f93cSkardel  *
192abb0f93cSkardel  * (The position of the rear pointer, rptr, is really 0 (as explained above
193abb0f93cSkardel  * in the initialization of randtbl) because the state table pointer is set
194abb0f93cSkardel  * to point to randtbl[1] (as explained below).
195abb0f93cSkardel  */
196b8ecfcfeSchristos static unsigned long *fptr = &randtbl[SEP_3 + 1];
197b8ecfcfeSchristos static unsigned long *rptr = &randtbl[1];
198abb0f93cSkardel 
199abb0f93cSkardel /*
200abb0f93cSkardel  * The following things are the pointer to the state information table, the
201abb0f93cSkardel  * type of the current generator, the degree of the current polynomial being
202abb0f93cSkardel  * used, and the separation between the two pointers.  Note that for efficiency
203abb0f93cSkardel  * of random(), we remember the first location of the state information, not
204abb0f93cSkardel  * the zeroeth.  Hence it is valid to access state[-1], which is used to
205abb0f93cSkardel  * store the type of the R.N.G.  Also, we remember the last location, since
206abb0f93cSkardel  * this is more efficient than indexing every time to find the address of
207abb0f93cSkardel  * the last element to see if the front and rear pointers have wrapped.
208abb0f93cSkardel  */
209b8ecfcfeSchristos static unsigned long *state = &randtbl[1];
210abb0f93cSkardel static long rand_type = TYPE_3;
211abb0f93cSkardel static long rand_deg = DEG_3;
212abb0f93cSkardel static long rand_sep = SEP_3;
213b8ecfcfeSchristos static unsigned long *end_ptr = &randtbl[DEG_3 + 1];
214abb0f93cSkardel 
215abb0f93cSkardel static inline long good_rand (long);
216abb0f93cSkardel 
217abb0f93cSkardel static inline long
218abb0f93cSkardel good_rand (
219abb0f93cSkardel 	register long x
220abb0f93cSkardel 	)
221abb0f93cSkardel {
222abb0f93cSkardel #ifdef  USE_WEAK_SEEDING
223abb0f93cSkardel /*
224abb0f93cSkardel  * Historic implementation compatibility.
225abb0f93cSkardel  * The random sequences do not vary much with the seed,
226abb0f93cSkardel  * even with overflowing.
227abb0f93cSkardel  */
228abb0f93cSkardel 	return (1103515245 * x + 12345);
229abb0f93cSkardel #else   /* !USE_WEAK_SEEDING */
230abb0f93cSkardel /*
231abb0f93cSkardel  * Compute x = (7^5 * x) mod (2^31 - 1)
232abb0f93cSkardel  * wihout overflowing 31 bits:
233abb0f93cSkardel  *      (2^31 - 1) = 127773 * (7^5) + 2836
234abb0f93cSkardel  * From "Random number generators: good ones are hard to find",
235abb0f93cSkardel  * Park and Miller, Communications of the ACM, vol. 31, no. 10,
236abb0f93cSkardel  * October 1988, p. 1195.
237abb0f93cSkardel  */
238abb0f93cSkardel 	register long hi, lo;
239abb0f93cSkardel 
240abb0f93cSkardel 	hi = x / 127773;
241abb0f93cSkardel 	lo = x % 127773;
242abb0f93cSkardel 	x = 16807 * lo - 2836 * hi;
243abb0f93cSkardel 	if (x <= 0)
244abb0f93cSkardel 		x += 0x7fffffff;
245abb0f93cSkardel 	return (x);
246abb0f93cSkardel #endif  /* !USE_WEAK_SEEDING */
247abb0f93cSkardel }
248abb0f93cSkardel 
249abb0f93cSkardel /*
250abb0f93cSkardel  * srandom:
251abb0f93cSkardel  *
252abb0f93cSkardel  * Initialize the random number generator based on the given seed.  If the
253abb0f93cSkardel  * type is the trivial no-state-information type, just remember the seed.
254abb0f93cSkardel  * Otherwise, initializes state[] based on the given "seed" via a linear
255abb0f93cSkardel  * congruential generator.  Then, the pointers are set to known locations
256abb0f93cSkardel  * that are exactly rand_sep places apart.  Lastly, it cycles the state
257abb0f93cSkardel  * information a given number of times to get rid of any initial dependencies
258abb0f93cSkardel  * introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
259abb0f93cSkardel  * for default usage relies on values produced by this routine.
260abb0f93cSkardel  */
261abb0f93cSkardel void
262abb0f93cSkardel ntp_srandom(
263abb0f93cSkardel 	unsigned long x
264abb0f93cSkardel 	)
265abb0f93cSkardel {
2668585484eSchristos 	long i;
267abb0f93cSkardel 
2688585484eSchristos 	if (rand_type == TYPE_0) {
269abb0f93cSkardel 		state[0] = x;
2708585484eSchristos 	} else {
271abb0f93cSkardel 		state[0] = x;
272abb0f93cSkardel 		for (i = 1; i < rand_deg; i++)
273abb0f93cSkardel 			state[i] = good_rand(state[i - 1]);
274abb0f93cSkardel 		fptr = &state[rand_sep];
275abb0f93cSkardel 		rptr = &state[0];
276abb0f93cSkardel 		for (i = 0; i < 10 * rand_deg; i++)
2778585484eSchristos 			x = ntp_random();
278abb0f93cSkardel 	}
2798585484eSchristos 
2808585484eSchristos 	/* seed the likely faster (and poorer) rand() as well */
2818585484eSchristos 	srand((u_int)x);
282abb0f93cSkardel }
283abb0f93cSkardel 
284abb0f93cSkardel /*
285abb0f93cSkardel  * srandomdev:
286abb0f93cSkardel  *
287abb0f93cSkardel  * Many programs choose the seed value in a totally predictable manner.
288abb0f93cSkardel  * This often causes problems.  We seed the generator using the much more
289abb0f93cSkardel  * secure urandom(4) interface.  Note that this particular seeding
290abb0f93cSkardel  * procedure can generate states which are impossible to reproduce by
291abb0f93cSkardel  * calling srandom() with any value, since the succeeding terms in the
292abb0f93cSkardel  * state buffer are no longer derived from the LC algorithm applied to
293abb0f93cSkardel  * a fixed seed.
294abb0f93cSkardel  */
295abb0f93cSkardel #ifdef NEED_SRANDOMDEV
296abb0f93cSkardel void
297abb0f93cSkardel ntp_srandomdev( void )
298abb0f93cSkardel {
299abb0f93cSkardel 	struct timeval tv;
300abb0f93cSkardel 	unsigned long junk;	/* Purposely used uninitialized */
301abb0f93cSkardel 
302abb0f93cSkardel 	GETTIMEOFDAY(&tv, NULL);
303abb0f93cSkardel 	ntp_srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk);
304abb0f93cSkardel 	return;
305abb0f93cSkardel }
306abb0f93cSkardel #endif
307abb0f93cSkardel 
3088585484eSchristos 
3098585484eSchristos /*
3108585484eSchristos  * ntp_initstate() and ntp_setstate() are unused in our codebase and
3118585484eSchristos  * trigger warnings due to casting to a more-strictly-aligned pointer
3128585484eSchristos  * on alignment-sensitive platforms.  #ifdef them away to save noise,
3138585484eSchristos  * build time, and binary space, but retain the code in case we find a
3148585484eSchristos  * use.
3158585484eSchristos  */
3168585484eSchristos #ifdef COMPILE_UNUSED_FUNCTIONS
3178585484eSchristos /*
3188585484eSchristos  * Array versions of the above information to make code run faster --
3198585484eSchristos  * relies on fact that TYPE_i == i.
3208585484eSchristos  */
3218585484eSchristos #define	MAX_TYPES	5		/* max number of types above */
3228585484eSchristos 
3238585484eSchristos static long degrees[MAX_TYPES] =	{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
3248585484eSchristos static long seps [MAX_TYPES] =	{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
3258585484eSchristos 
326abb0f93cSkardel /*
327abb0f93cSkardel  * initstate:
328abb0f93cSkardel  *
329abb0f93cSkardel  * Initialize the state information in the given array of n bytes for future
330abb0f93cSkardel  * random number generation.  Based on the number of bytes we are given, and
331abb0f93cSkardel  * the break values for the different R.N.G.'s, we choose the best (largest)
332abb0f93cSkardel  * one we can and set things up for it.  srandom() is then called to
333abb0f93cSkardel  * initialize the state information.
334abb0f93cSkardel  *
335abb0f93cSkardel  * Note that on return from srandom(), we set state[-1] to be the type
336abb0f93cSkardel  * multiplexed with the current value of the rear pointer; this is so
337abb0f93cSkardel  * successive calls to initstate() won't lose this information and will be
338abb0f93cSkardel  * able to restart with setstate().
339abb0f93cSkardel  *
340abb0f93cSkardel  * Note: the first thing we do is save the current state, if any, just like
341abb0f93cSkardel  * setstate() so that it doesn't matter when initstate is called.
342abb0f93cSkardel  *
343abb0f93cSkardel  * Returns a pointer to the old state.
344abb0f93cSkardel  *
345abb0f93cSkardel  * Note: The Sparc platform requires that arg_state begin on a long
346abb0f93cSkardel  * word boundary; otherwise a bus error will occur. Even so, lint will
347abb0f93cSkardel  * complain about mis-alignment, but you should disregard these messages.
348abb0f93cSkardel  */
349abb0f93cSkardel char *
350abb0f93cSkardel ntp_initstate(
351abb0f93cSkardel 	unsigned long seed,		/* seed for R.N.G. */
352abb0f93cSkardel 	char *arg_state,		/* pointer to state array */
353abb0f93cSkardel 	long n				/* # bytes of state info */
354abb0f93cSkardel 	)
355abb0f93cSkardel {
356abb0f93cSkardel 	register char *ostate = (char *)(&state[-1]);
357abb0f93cSkardel 	register long *long_arg_state = (long *) arg_state;
358abb0f93cSkardel 
359abb0f93cSkardel 	if (rand_type == TYPE_0)
360abb0f93cSkardel 		state[-1] = rand_type;
361abb0f93cSkardel 	else
362abb0f93cSkardel 		state[-1] = MAX_TYPES * (rptr - state) + rand_type;
363abb0f93cSkardel 	if (n < BREAK_0) {
364abb0f93cSkardel 		(void)fprintf(stderr,
365abb0f93cSkardel 		    "random: not enough state (%ld bytes); ignored.\n", n);
366abb0f93cSkardel 		return(0);
367abb0f93cSkardel 	}
368abb0f93cSkardel 	if (n < BREAK_1) {
369abb0f93cSkardel 		rand_type = TYPE_0;
370abb0f93cSkardel 		rand_deg = DEG_0;
371abb0f93cSkardel 		rand_sep = SEP_0;
372abb0f93cSkardel 	} else if (n < BREAK_2) {
373abb0f93cSkardel 		rand_type = TYPE_1;
374abb0f93cSkardel 		rand_deg = DEG_1;
375abb0f93cSkardel 		rand_sep = SEP_1;
376abb0f93cSkardel 	} else if (n < BREAK_3) {
377abb0f93cSkardel 		rand_type = TYPE_2;
378abb0f93cSkardel 		rand_deg = DEG_2;
379abb0f93cSkardel 		rand_sep = SEP_2;
380abb0f93cSkardel 	} else if (n < BREAK_4) {
381abb0f93cSkardel 		rand_type = TYPE_3;
382abb0f93cSkardel 		rand_deg = DEG_3;
383abb0f93cSkardel 		rand_sep = SEP_3;
384abb0f93cSkardel 	} else {
385abb0f93cSkardel 		rand_type = TYPE_4;
386abb0f93cSkardel 		rand_deg = DEG_4;
387abb0f93cSkardel 		rand_sep = SEP_4;
388abb0f93cSkardel 	}
389b8ecfcfeSchristos 	state = (unsigned long *) (long_arg_state + 1); /* first location */
390abb0f93cSkardel 	end_ptr = &state[rand_deg];	/* must set end_ptr before srandom */
391abb0f93cSkardel 	ntp_srandom(seed);
392abb0f93cSkardel 	if (rand_type == TYPE_0)
393abb0f93cSkardel 		long_arg_state[0] = rand_type;
394abb0f93cSkardel 	else
395abb0f93cSkardel 		long_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type;
396abb0f93cSkardel 	return(ostate);
397abb0f93cSkardel }
398abb0f93cSkardel 
399abb0f93cSkardel /*
400abb0f93cSkardel  * setstate:
401abb0f93cSkardel  *
402abb0f93cSkardel  * Restore the state from the given state array.
403abb0f93cSkardel  *
404abb0f93cSkardel  * Note: it is important that we also remember the locations of the pointers
405abb0f93cSkardel  * in the current state information, and restore the locations of the pointers
406abb0f93cSkardel  * from the old state information.  This is done by multiplexing the pointer
407abb0f93cSkardel  * location into the zeroeth word of the state information.
408abb0f93cSkardel  *
409abb0f93cSkardel  * Note that due to the order in which things are done, it is OK to call
410abb0f93cSkardel  * setstate() with the same state as the current state.
411abb0f93cSkardel  *
412abb0f93cSkardel  * Returns a pointer to the old state information.
413abb0f93cSkardel  *
414abb0f93cSkardel  * Note: The Sparc platform requires that arg_state begin on a long
415abb0f93cSkardel  * word boundary; otherwise a bus error will occur. Even so, lint will
416abb0f93cSkardel  * complain about mis-alignment, but you should disregard these messages.
417abb0f93cSkardel  */
418abb0f93cSkardel char *
419abb0f93cSkardel ntp_setstate(
420abb0f93cSkardel 	char *arg_state			/* pointer to state array */
421abb0f93cSkardel 	)
422abb0f93cSkardel {
423b8ecfcfeSchristos 	register unsigned long *new_state = (unsigned long *) arg_state;
424abb0f93cSkardel 	register long type = new_state[0] % MAX_TYPES;
425abb0f93cSkardel 	register long rear = new_state[0] / MAX_TYPES;
426abb0f93cSkardel 	char *ostate = (char *)(&state[-1]);
427abb0f93cSkardel 
428abb0f93cSkardel 	if (rand_type == TYPE_0)
429abb0f93cSkardel 		state[-1] = rand_type;
430abb0f93cSkardel 	else
431abb0f93cSkardel 		state[-1] = MAX_TYPES * (rptr - state) + rand_type;
432abb0f93cSkardel 	switch(type) {
433abb0f93cSkardel 	case TYPE_0:
434abb0f93cSkardel 	case TYPE_1:
435abb0f93cSkardel 	case TYPE_2:
436abb0f93cSkardel 	case TYPE_3:
437abb0f93cSkardel 	case TYPE_4:
438abb0f93cSkardel 		rand_type = type;
439abb0f93cSkardel 		rand_deg = degrees[type];
440abb0f93cSkardel 		rand_sep = seps[type];
441abb0f93cSkardel 		break;
442abb0f93cSkardel 	default:
443abb0f93cSkardel 		(void)fprintf(stderr,
444abb0f93cSkardel 		    "random: state info corrupted; not changed.\n");
445abb0f93cSkardel 	}
446b8ecfcfeSchristos 	state = (new_state + 1);
447abb0f93cSkardel 	if (rand_type != TYPE_0) {
448abb0f93cSkardel 		rptr = &state[rear];
449abb0f93cSkardel 		fptr = &state[(rear + rand_sep) % rand_deg];
450abb0f93cSkardel 	}
451abb0f93cSkardel 	end_ptr = &state[rand_deg];		/* set end_ptr too */
452abb0f93cSkardel 	return(ostate);
453abb0f93cSkardel }
4548585484eSchristos #endif	/* COMPILE_UNUSED_FUNCTIONS */
4558585484eSchristos 
456abb0f93cSkardel 
457abb0f93cSkardel /*
458abb0f93cSkardel  * random:
459abb0f93cSkardel  *
460abb0f93cSkardel  * If we are using the trivial TYPE_0 R.N.G., just do the old linear
461abb0f93cSkardel  * congruential bit.  Otherwise, we do our fancy trinomial stuff, which is
462abb0f93cSkardel  * the same in all the other cases due to all the global variables that have
463abb0f93cSkardel  * been set up.  The basic operation is to add the number at the rear pointer
464abb0f93cSkardel  * into the one at the front pointer.  Then both pointers are advanced to
465abb0f93cSkardel  * the next location cyclically in the table.  The value returned is the sum
466abb0f93cSkardel  * generated, reduced to 31 bits by throwing away the "least random" low bit.
467abb0f93cSkardel  *
468abb0f93cSkardel  * Note: the code takes advantage of the fact that both the front and
469abb0f93cSkardel  * rear pointers can't wrap on the same call by not testing the rear
470abb0f93cSkardel  * pointer if the front one has wrapped.
471abb0f93cSkardel  *
472abb0f93cSkardel  * Returns a 31-bit random number.
473abb0f93cSkardel  */
474abb0f93cSkardel long
475abb0f93cSkardel ntp_random( void )
476abb0f93cSkardel {
477abb0f93cSkardel 	register long i;
478b8ecfcfeSchristos 	register unsigned long *f, *r;
479abb0f93cSkardel 
480abb0f93cSkardel 	if (rand_type == TYPE_0) {
481abb0f93cSkardel 		i = state[0];
482abb0f93cSkardel 		state[0] = i = (good_rand(i)) & 0x7fffffff;
483abb0f93cSkardel 	} else {
484abb0f93cSkardel 		/*
485abb0f93cSkardel 		 * Use local variables rather than static variables for speed.
486abb0f93cSkardel 		 */
487abb0f93cSkardel 		f = fptr; r = rptr;
488abb0f93cSkardel 		*f += *r;
489abb0f93cSkardel 		i = (*f >> 1) & 0x7fffffff;	/* chucking least random bit */
490abb0f93cSkardel 		if (++f >= end_ptr) {
491abb0f93cSkardel 			f = state;
492abb0f93cSkardel 			++r;
493abb0f93cSkardel 		}
494abb0f93cSkardel 		else if (++r >= end_ptr) {
495abb0f93cSkardel 			r = state;
496abb0f93cSkardel 		}
497abb0f93cSkardel 
498abb0f93cSkardel 		fptr = f; rptr = r;
499abb0f93cSkardel 	}
500abb0f93cSkardel 	return(i);
501abb0f93cSkardel }
502*eabc0478Schristos 
503*eabc0478Schristos /*
504*eabc0478Schristos  * ntp_uurandom()
505*eabc0478Schristos  *
506*eabc0478Schristos  * Generate a Uniform-distributed Unity based random number. Replaces a
507*eabc0478Schristos  * few locations where the transformation was made in an ad-hoc style
508*eabc0478Schristos  * (and in one instance, wrong...)
509*eabc0478Schristos  *
510*eabc0478Schristos  * returns a number in [0.0 .. 1.0], both ends inclusive
511*eabc0478Schristos  */
512*eabc0478Schristos double
513*eabc0478Schristos ntp_uurandom( void )
514*eabc0478Schristos {
515*eabc0478Schristos 	return (double)ntp_random() / 0x7FFFFFFFu;
516*eabc0478Schristos }
517