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