11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1992, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 291541Srgrimes * @(#)random.c 8.1 (Berkeley) 6/10/93 301541Srgrimes */ 311541Srgrimes 32116189Sobrien#include <sys/cdefs.h> 33116189Sobrien__FBSDID("$FreeBSD: stable/10/sys/libkern/random.c 319286 2017-05-31 06:00:14Z delphij $"); 34116189Sobrien 357109Sphk#include <sys/libkern.h> 361541Srgrimes 37118732Sache#define NSHUFF 50 /* to drop some "seed -> 1st value" linearity */ 38110322Sache 39118732Sachestatic u_long randseed = 937186357; /* after srandom(1), NSHUFF counted */ 4018474Speter 4118474Spetervoid 4218474Spetersrandom(seed) 4318474Speter u_long seed; 4418474Speter{ 45110322Sache int i; 46110322Sache 4718474Speter randseed = seed; 48110322Sache for (i = 0; i < NSHUFF; i++) 49110322Sache (void)random(); 5018474Speter} 5118474Speter 521541Srgrimes/* 531541Srgrimes * Pseudo-random number generator for randomizing the profiling clock, 541541Srgrimes * and whatever else we might use it for. The result is uniform on 551541Srgrimes * [0, 2^31 - 1]. 561541Srgrimes */ 571541Srgrimesu_long 581541Srgrimesrandom() 591541Srgrimes{ 60319286Sdelphij long x, hi, lo, t; 611541Srgrimes 621541Srgrimes /* 631541Srgrimes * Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1). 641541Srgrimes * From "Random number generators: good ones are hard to find", 651541Srgrimes * Park and Miller, Communications of the ACM, vol. 31, no. 10, 661541Srgrimes * October 1988, p. 1195. 671541Srgrimes */ 68110281Sache /* Can't be initialized with 0, so use another value. */ 69110281Sache if ((x = randseed) == 0) 70110281Sache x = 123459876; 711541Srgrimes hi = x / 127773; 721541Srgrimes lo = x % 127773; 731541Srgrimes t = 16807 * lo - 2836 * hi; 74110281Sache if (t < 0) 751541Srgrimes t += 0x7fffffff; 761541Srgrimes randseed = t; 771541Srgrimes return (t); 781541Srgrimes} 79