1*48349Scael.\" Copyright (c) 1983, 1991 The Regents of the University of California. 238479Sbostic.\" All rights reserved. 320578Smckusick.\" 443571Strent.\" %sccs.include.redist.man% 520578Smckusick.\" 6*48349Scael.\" @(#)random.3 6.5 (Berkeley) 04/19/91 738479Sbostic.\" 8*48349Scael.Dd 9*48349Scael.Dt RANDOM 3 10*48349Scael.Os BSD 4.2 11*48349Scael.Sh NAME 12*48349Scael.Nm random , 13*48349Scael.Nm srandom , 14*48349Scael.Nm initstate , 15*48349Scael.Nm setstate 16*48349Scael.Nd better random number generator; routines for changing generators 17*48349Scael.Sh SYNOPSIS 18*48349Scael.Fd #include <stdlib> 19*48349Scael.Ft long 20*48349Scael.Fn random void 21*48349Scael.Ft void 22*48349Scael.Fn srandom "unsigned seed" 23*48349Scael.Ft char * 24*48349Scael.Fn initstate "unsigned seed" "char *state" "int n" 25*48349Scael.Ft char * 26*48349Scael.Fn setstate "char *state" 27*48349Scael.Sh DESCRIPTION 28*48349ScaelThe 29*48349Scael.Fn random 30*48349Scaelfunction 3120578Smckusickuses a non-linear additive feedback random number generator employing a 3220578Smckusickdefault table of size 31 long integers to return successive pseudo-random 3325048Smckusicknumbers in the range from 0 to 3425048Smckusick.if t 2\u\s731\s10\d\(mi1. 3525048Smckusick.if n (2**31)\(mi1. 3625048SmckusickThe period of this random number generator is very large, approximately 3725048Smckusick.if t 16\(mu(2\u\s731\s10\d\(mi1). 3825048Smckusick.if n 16*((2**31)\(mi1). 39*48349Scael.Pp 40*48349ScaelThe 41*48349Scael.Fn random Ns / Fn srandom 4220578Smckusickhave (almost) the same calling sequence and initialization properties as 43*48349Scael.Xr rand 3 Ns / Xr srand 3 . 4420578SmckusickThe difference is that 45*48349Scael.Xr rand 4625048Smckusickproduces a much less random sequence \(em in fact, the low dozen bits 4720578Smckusickgenerated by rand go through a cyclic pattern. All the bits generated by 48*48349Scael.Fn random 49*48349Scaelare usable. For example, 50*48349Scael.Sq Li random()&01 51*48349Scaelwill produce a random binary 5220578Smckusickvalue. 53*48349Scael.Pp 5420578SmckusickUnlike 55*48349Scael.Xr srand , 56*48349Scael.Fn srandom 5720578Smckusickdoes not return the old seed; the reason for this is that the amount of 5820578Smckusickstate information used is much more than a single word. (Two other 5920578Smckusickroutines are provided to deal with restarting/changing random 6020578Smckusicknumber generators). Like 61*48349Scael.Xr rand 3 , 6220578Smckusickhowever, 63*48349Scael.Fn random 6420578Smckusickwill by default produce a sequence of numbers that can be duplicated 6520578Smckusickby calling 66*48349Scael.Fn srandom 6720578Smckusickwith 68*48349Scael.Ql 1 6920578Smckusickas the seed. 70*48349Scael.Pp 7120578SmckusickThe 72*48349Scael.Fn initstate 7320578Smckusickroutine allows a state array, passed in as an argument, to be initialized 7420578Smckusickfor future use. The size of the state array (in bytes) is used by 75*48349Scael.Fn initstate 76*48349Scaelto decide how sophisticated a random number generator it should use \(em the 7720578Smckusickmore state, the better the random numbers will be. 7820578Smckusick(Current "optimal" values for the amount of state information are 7920578Smckusick8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to 80*48349Scaelthe nearest known amount. Using less than 8 bytes will cause an error.) 8120578SmckusickThe seed for the initialization (which specifies a starting point for 8220578Smckusickthe random number sequence, and provides for restarting at the same 8320578Smckusickpoint) is also an argument. 84*48349ScaelThe 85*48349Scael.Fn initstate 86*48349Scaelfunction 8720578Smckusickreturns a pointer to the previous state information array. 88*48349Scael.Pp 8920578SmckusickOnce a state has been initialized, the 90*48349Scael.Fn setstate 9120578Smckusickroutine provides for rapid switching between states. 92*48349ScaelThe 93*48349Scael.Fn setstate 94*48349Scaelfunction 9520579Smckusickreturns a pointer to the previous state array; its 9620578Smckusickargument state array is used for further random number generation 9720578Smckusickuntil the next call to 98*48349Scael.Fn initstate 9920578Smckusickor 100*48349Scael.Fn setstate . 101*48349Scael.Pp 10220578SmckusickOnce a state array has been initialized, it may be restarted at a 10320578Smckusickdifferent point either by calling 104*48349Scael.Fn initstate 10520578Smckusick(with the desired seed, the state array, and its size) or by calling 10620578Smckusickboth 107*48349Scael.Fn setstate 10820578Smckusick(with the state array) and 109*48349Scael.Fn srandom 11020578Smckusick(with the desired seed). 11120578SmckusickThe advantage of calling both 112*48349Scael.Fn setstate 11320578Smckusickand 114*48349Scael.Fn srandom 11520578Smckusickis that the size of the state array does not have to be remembered after 11620578Smckusickit is initialized. 117*48349Scael.Pp 11820578SmckusickWith 256 bytes of state information, the period of the random number 11925048Smckusickgenerator is greater than 12025048Smckusick.if t 2\u\s769\s10\d, 12125048Smckusick.if n 2**69 12225048Smckusickwhich should be sufficient for most purposes. 123*48349Scael.Sh AUTHOR 12420578SmckusickEarl T. Cohen 125*48349Scael.Sh DIAGNOSTICS 12620578SmckusickIf 127*48349Scael.Fn initstate 12820578Smckusickis called with less than 8 bytes of state information, or if 129*48349Scael.Fn setstate 13020578Smckusickdetects that the state information has been garbled, error 13120578Smckusickmessages are printed on the standard error output. 132*48349Scael.Sh SEE ALSO 133*48349Scael.Xr rand 3 134*48349Scael.Sh HISTORY 135*48349ScaelThese 136*48349Scaelfunctions appeared in 137*48349Scael.Bx 4.2 . 138*48349Scael.Sh BUGS 13920578SmckusickAbout 2/3 the speed of 140*48349Scael.Xr rand 3 . 141