154353Storek /*- 254353Storek * Copyright (c) 1992 The Regents of the University of California. 354353Storek * All rights reserved. 454353Storek * 554353Storek * %sccs.include.redist.c% 654353Storek * 7*56518Sbostic * @(#)random.c 7.2 (Berkeley) 10/11/92 854353Storek */ 954353Storek 10*56518Sbostic #include <libkern/libkern.h> 1154353Storek 1254353Storek /* 1354353Storek * Pseudo-random number generator for randomizing the profiling clock, 1454353Storek * and whatever else we might use it for. The result is uniform on 1554353Storek * [0, 2^31 - 1]. 1654353Storek */ 1754353Storek u_long 1854353Storek random() 1954353Storek { 2054353Storek static u_long randseed = 1; 2154353Storek register long x, hi, lo, t; 2254353Storek 2354353Storek /* 2454353Storek * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1). 2554353Storek * From "Random number generators: good ones are hard to find", 2654353Storek * Park and Miller, Communications of the ACM, vol. 31, no. 10, 2754353Storek * October 1988, p. 1195. 2854353Storek */ 2954353Storek x = randseed; 3054353Storek hi = x / 127773; 3154353Storek lo = x % 127773; 3254353Storek t = 16807 * lo - 2836 * hi; 3354353Storek if (t <= 0) 3454353Storek t += 0x7fffffff; 3554353Storek randseed = t; 3654353Storek return (t); 3754353Storek } 38