138451Smsmith/*-
238451Smsmith * Copyright (c) 1992, 1993
338451Smsmith *	The Regents of the University of California.  All rights reserved.
438451Smsmith *
538451Smsmith * Redistribution and use in source and binary forms, with or without
638451Smsmith * modification, are permitted provided that the following conditions
738451Smsmith * are met:
838451Smsmith * 1. Redistributions of source code must retain the above copyright
938451Smsmith *    notice, this list of conditions and the following disclaimer.
1038451Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1138451Smsmith *    notice, this list of conditions and the following disclaimer in the
1238451Smsmith *    documentation and/or other materials provided with the distribution.
1338451Smsmith * 4. Neither the name of the University nor the names of its contributors
1438451Smsmith *    may be used to endorse or promote products derived from this software
1538451Smsmith *    without specific prior written permission.
1638451Smsmith *
1738451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1838451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1938451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2038451Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2138451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2238451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2338451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2438451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2538451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2638451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2738451Smsmith * SUCH DAMAGE.
2838451Smsmith *
2938451Smsmith *	@(#)random.c	8.1 (Berkeley) 6/10/93
3038451Smsmith */
3138451Smsmith
3284221Sdillon#include <sys/cdefs.h>
3384221Sdillon__FBSDID("$FreeBSD$");
3484221Sdillon
3565400Speter#include <sys/types.h>
3638451Smsmith
3738451Smsmithstatic u_long randseed = 1;
3838451Smsmith
3938451Smsmithvoid
4038451Smsmithsrandom(seed)
4138451Smsmith	u_long seed;
4238451Smsmith{
4338451Smsmith	randseed = seed;
4438451Smsmith}
4538451Smsmith
4638451Smsmith/*
4738451Smsmith * Pseudo-random number generator for randomizing the profiling clock,
4838451Smsmith * and whatever else we might use it for.  The result is uniform on
4938451Smsmith * [0, 2^31 - 1].
5038451Smsmith */
5138451Smsmithu_long
5238451Smsmithrandom()
5338451Smsmith{
5492913Sobrien	long x, hi, lo, t;
5538451Smsmith
5638451Smsmith	/*
5738451Smsmith	 * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1).
5838451Smsmith	 * From "Random number generators: good ones are hard to find",
5938451Smsmith	 * Park and Miller, Communications of the ACM, vol. 31, no. 10,
6038451Smsmith	 * October 1988, p. 1195.
6138451Smsmith	 */
6238451Smsmith	x = randseed;
6338451Smsmith	hi = x / 127773;
6438451Smsmith	lo = x % 127773;
6538451Smsmith	t = 16807 * lo - 2836 * hi;
6638451Smsmith	if (t <= 0)
6738451Smsmith		t += 0x7fffffff;
6838451Smsmith	randseed = t;
6938451Smsmith	return (t);
7038451Smsmith}
71