xref: /csrg-svn/sys/libkern/random.c (revision 54353)
1*54353Storek /*-
2*54353Storek  * Copyright (c) 1992 The Regents of the University of California.
3*54353Storek  * All rights reserved.
4*54353Storek  *
5*54353Storek  * %sccs.include.redist.c%
6*54353Storek  *
7*54353Storek  *	@(#)random.c	7.1 (Berkeley) 06/24/92
8*54353Storek  */
9*54353Storek 
10*54353Storek #include "libkern.h"
11*54353Storek 
12*54353Storek /*
13*54353Storek  * Pseudo-random number generator for randomizing the profiling clock,
14*54353Storek  * and whatever else we might use it for.  The result is uniform on
15*54353Storek  * [0, 2^31 - 1].
16*54353Storek  */
17*54353Storek u_long
18*54353Storek random()
19*54353Storek {
20*54353Storek 	static u_long randseed = 1;
21*54353Storek 	register long x, hi, lo, t;
22*54353Storek 
23*54353Storek 	/*
24*54353Storek 	 * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1).
25*54353Storek 	 * From "Random number generators: good ones are hard to find",
26*54353Storek 	 * Park and Miller, Communications of the ACM, vol. 31, no. 10,
27*54353Storek 	 * October 1988, p. 1195.
28*54353Storek 	 */
29*54353Storek 	x = randseed;
30*54353Storek 	hi = x / 127773;
31*54353Storek 	lo = x % 127773;
32*54353Storek 	t = 16807 * lo - 2836 * hi;
33*54353Storek 	if (t <= 0)
34*54353Storek 		t += 0x7fffffff;
35*54353Storek 	randseed = t;
36*54353Storek 	return (t);
37*54353Storek }
38