1*58f0484fSRodney W. Grimes.\" Copyright (c) 1983, 1991, 1993 2*58f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 3*58f0484fSRodney W. Grimes.\" 4*58f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 5*58f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 6*58f0484fSRodney W. Grimes.\" are met: 7*58f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 8*58f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 9*58f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 10*58f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 11*58f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12*58f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software 13*58f0484fSRodney W. Grimes.\" must display the following acknowledgement: 14*58f0484fSRodney W. Grimes.\" This product includes software developed by the University of 15*58f0484fSRodney W. Grimes.\" California, Berkeley and its contributors. 16*58f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors 17*58f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 18*58f0484fSRodney W. Grimes.\" without specific prior written permission. 19*58f0484fSRodney W. Grimes.\" 20*58f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21*58f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22*58f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23*58f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24*58f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25*58f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26*58f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27*58f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28*58f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29*58f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30*58f0484fSRodney W. Grimes.\" SUCH DAMAGE. 31*58f0484fSRodney W. Grimes.\" 32*58f0484fSRodney W. Grimes.\" @(#)random.3 8.1 (Berkeley) 6/4/93 33*58f0484fSRodney W. Grimes.\" 34*58f0484fSRodney W. Grimes.Dd June 4, 1993 35*58f0484fSRodney W. Grimes.Dt RANDOM 3 36*58f0484fSRodney W. Grimes.Os BSD 4.2 37*58f0484fSRodney W. Grimes.Sh NAME 38*58f0484fSRodney W. Grimes.Nm random , 39*58f0484fSRodney W. Grimes.Nm srandom , 40*58f0484fSRodney W. Grimes.Nm initstate , 41*58f0484fSRodney W. Grimes.Nm setstate 42*58f0484fSRodney W. Grimes.Nd better random number generator; routines for changing generators 43*58f0484fSRodney W. Grimes.Sh SYNOPSIS 44*58f0484fSRodney W. Grimes.Fd #include <stdlib.h> 45*58f0484fSRodney W. Grimes.Ft long 46*58f0484fSRodney W. Grimes.Fn random void 47*58f0484fSRodney W. Grimes.Ft void 48*58f0484fSRodney W. Grimes.Fn srandom "unsigned seed" 49*58f0484fSRodney W. Grimes.Ft char * 50*58f0484fSRodney W. Grimes.Fn initstate "unsigned seed" "char *state" "int n" 51*58f0484fSRodney W. Grimes.Ft char * 52*58f0484fSRodney W. Grimes.Fn setstate "char *state" 53*58f0484fSRodney W. Grimes.Sh DESCRIPTION 54*58f0484fSRodney W. GrimesThe 55*58f0484fSRodney W. Grimes.Fn random 56*58f0484fSRodney W. Grimesfunction 57*58f0484fSRodney W. Grimesuses a non-linear additive feedback random number generator employing a 58*58f0484fSRodney W. Grimesdefault table of size 31 long integers to return successive pseudo-random 59*58f0484fSRodney W. Grimesnumbers in the range from 0 to 60*58f0484fSRodney W. Grimes.if t 2\u\s731\s10\d\(mi1. 61*58f0484fSRodney W. Grimes.if n (2**31)\(mi1. 62*58f0484fSRodney W. GrimesThe period of this random number generator is very large, approximately 63*58f0484fSRodney W. Grimes.if t 16\(mu(2\u\s731\s10\d\(mi1). 64*58f0484fSRodney W. Grimes.if n 16*((2**31)\(mi1). 65*58f0484fSRodney W. Grimes.Pp 66*58f0484fSRodney W. GrimesThe 67*58f0484fSRodney W. Grimes.Fn random Ns / Fn srandom 68*58f0484fSRodney W. Grimeshave (almost) the same calling sequence and initialization properties as 69*58f0484fSRodney W. Grimes.Xr rand 3 Ns / Xr srand 3 . 70*58f0484fSRodney W. GrimesThe difference is that 71*58f0484fSRodney W. Grimes.Xr rand 72*58f0484fSRodney W. Grimesproduces a much less random sequence \(em in fact, the low dozen bits 73*58f0484fSRodney W. Grimesgenerated by rand go through a cyclic pattern. All the bits generated by 74*58f0484fSRodney W. Grimes.Fn random 75*58f0484fSRodney W. Grimesare usable. For example, 76*58f0484fSRodney W. Grimes.Sq Li random()&01 77*58f0484fSRodney W. Grimeswill produce a random binary 78*58f0484fSRodney W. Grimesvalue. 79*58f0484fSRodney W. Grimes.Pp 80*58f0484fSRodney W. GrimesUnlike 81*58f0484fSRodney W. Grimes.Xr srand , 82*58f0484fSRodney W. Grimes.Fn srandom 83*58f0484fSRodney W. Grimesdoes not return the old seed; the reason for this is that the amount of 84*58f0484fSRodney W. Grimesstate information used is much more than a single word. (Two other 85*58f0484fSRodney W. Grimesroutines are provided to deal with restarting/changing random 86*58f0484fSRodney W. Grimesnumber generators). Like 87*58f0484fSRodney W. Grimes.Xr rand 3 , 88*58f0484fSRodney W. Grimeshowever, 89*58f0484fSRodney W. Grimes.Fn random 90*58f0484fSRodney W. Grimeswill by default produce a sequence of numbers that can be duplicated 91*58f0484fSRodney W. Grimesby calling 92*58f0484fSRodney W. Grimes.Fn srandom 93*58f0484fSRodney W. Grimeswith 94*58f0484fSRodney W. Grimes.Ql 1 95*58f0484fSRodney W. Grimesas the seed. 96*58f0484fSRodney W. Grimes.Pp 97*58f0484fSRodney W. GrimesThe 98*58f0484fSRodney W. Grimes.Fn initstate 99*58f0484fSRodney W. Grimesroutine allows a state array, passed in as an argument, to be initialized 100*58f0484fSRodney W. Grimesfor future use. The size of the state array (in bytes) is used by 101*58f0484fSRodney W. Grimes.Fn initstate 102*58f0484fSRodney W. Grimesto decide how sophisticated a random number generator it should use \(em the 103*58f0484fSRodney W. Grimesmore state, the better the random numbers will be. 104*58f0484fSRodney W. Grimes(Current "optimal" values for the amount of state information are 105*58f0484fSRodney W. Grimes8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to 106*58f0484fSRodney W. Grimesthe nearest known amount. Using less than 8 bytes will cause an error.) 107*58f0484fSRodney W. GrimesThe seed for the initialization (which specifies a starting point for 108*58f0484fSRodney W. Grimesthe random number sequence, and provides for restarting at the same 109*58f0484fSRodney W. Grimespoint) is also an argument. 110*58f0484fSRodney W. GrimesThe 111*58f0484fSRodney W. Grimes.Fn initstate 112*58f0484fSRodney W. Grimesfunction 113*58f0484fSRodney W. Grimesreturns a pointer to the previous state information array. 114*58f0484fSRodney W. Grimes.Pp 115*58f0484fSRodney W. GrimesOnce a state has been initialized, the 116*58f0484fSRodney W. Grimes.Fn setstate 117*58f0484fSRodney W. Grimesroutine provides for rapid switching between states. 118*58f0484fSRodney W. GrimesThe 119*58f0484fSRodney W. Grimes.Fn setstate 120*58f0484fSRodney W. Grimesfunction 121*58f0484fSRodney W. Grimesreturns a pointer to the previous state array; its 122*58f0484fSRodney W. Grimesargument state array is used for further random number generation 123*58f0484fSRodney W. Grimesuntil the next call to 124*58f0484fSRodney W. Grimes.Fn initstate 125*58f0484fSRodney W. Grimesor 126*58f0484fSRodney W. Grimes.Fn setstate . 127*58f0484fSRodney W. Grimes.Pp 128*58f0484fSRodney W. GrimesOnce a state array has been initialized, it may be restarted at a 129*58f0484fSRodney W. Grimesdifferent point either by calling 130*58f0484fSRodney W. Grimes.Fn initstate 131*58f0484fSRodney W. Grimes(with the desired seed, the state array, and its size) or by calling 132*58f0484fSRodney W. Grimesboth 133*58f0484fSRodney W. Grimes.Fn setstate 134*58f0484fSRodney W. Grimes(with the state array) and 135*58f0484fSRodney W. Grimes.Fn srandom 136*58f0484fSRodney W. Grimes(with the desired seed). 137*58f0484fSRodney W. GrimesThe advantage of calling both 138*58f0484fSRodney W. Grimes.Fn setstate 139*58f0484fSRodney W. Grimesand 140*58f0484fSRodney W. Grimes.Fn srandom 141*58f0484fSRodney W. Grimesis that the size of the state array does not have to be remembered after 142*58f0484fSRodney W. Grimesit is initialized. 143*58f0484fSRodney W. Grimes.Pp 144*58f0484fSRodney W. GrimesWith 256 bytes of state information, the period of the random number 145*58f0484fSRodney W. Grimesgenerator is greater than 146*58f0484fSRodney W. Grimes.if t 2\u\s769\s10\d, 147*58f0484fSRodney W. Grimes.if n 2**69 148*58f0484fSRodney W. Grimeswhich should be sufficient for most purposes. 149*58f0484fSRodney W. Grimes.Sh AUTHOR 150*58f0484fSRodney W. GrimesEarl T. Cohen 151*58f0484fSRodney W. Grimes.Sh DIAGNOSTICS 152*58f0484fSRodney W. GrimesIf 153*58f0484fSRodney W. Grimes.Fn initstate 154*58f0484fSRodney W. Grimesis called with less than 8 bytes of state information, or if 155*58f0484fSRodney W. Grimes.Fn setstate 156*58f0484fSRodney W. Grimesdetects that the state information has been garbled, error 157*58f0484fSRodney W. Grimesmessages are printed on the standard error output. 158*58f0484fSRodney W. Grimes.Sh SEE ALSO 159*58f0484fSRodney W. Grimes.Xr rand 3 160*58f0484fSRodney W. Grimes.Sh HISTORY 161*58f0484fSRodney W. GrimesThese 162*58f0484fSRodney W. Grimesfunctions appeared in 163*58f0484fSRodney W. Grimes.Bx 4.2 . 164*58f0484fSRodney W. Grimes.Sh BUGS 165*58f0484fSRodney W. GrimesAbout 2/3 the speed of 166*58f0484fSRodney W. Grimes.Xr rand 3 . 167