1*61180Sbostic.\" Copyright (c) 1983, 1991, 1993 2*61180Sbostic.\" The Regents of the University of California. All rights reserved. 320578Smckusick.\" 443571Strent.\" %sccs.include.redist.man% 520578Smckusick.\" 6*61180Sbostic.\" @(#)random.3 8.1 (Berkeley) 06/04/93 738479Sbostic.\" 848349Scael.Dd 948349Scael.Dt RANDOM 3 1048349Scael.Os BSD 4.2 1148349Scael.Sh NAME 1248349Scael.Nm random , 1348349Scael.Nm srandom , 1448349Scael.Nm initstate , 1548349Scael.Nm setstate 1648349Scael.Nd better random number generator; routines for changing generators 1748349Scael.Sh SYNOPSIS 1859201Shibler.Fd #include <stdlib.h> 1948349Scael.Ft long 2048349Scael.Fn random void 2148349Scael.Ft void 2248349Scael.Fn srandom "unsigned seed" 2348349Scael.Ft char * 2448349Scael.Fn initstate "unsigned seed" "char *state" "int n" 2548349Scael.Ft char * 2648349Scael.Fn setstate "char *state" 2748349Scael.Sh DESCRIPTION 2848349ScaelThe 2948349Scael.Fn random 3048349Scaelfunction 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). 3948349Scael.Pp 4048349ScaelThe 4148349Scael.Fn random Ns / Fn srandom 4220578Smckusickhave (almost) the same calling sequence and initialization properties as 4348349Scael.Xr rand 3 Ns / Xr srand 3 . 4420578SmckusickThe difference is that 4548349Scael.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 4848349Scael.Fn random 4948349Scaelare usable. For example, 5048349Scael.Sq Li random()&01 5148349Scaelwill produce a random binary 5220578Smckusickvalue. 5348349Scael.Pp 5420578SmckusickUnlike 5548349Scael.Xr srand , 5648349Scael.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 6148349Scael.Xr rand 3 , 6220578Smckusickhowever, 6348349Scael.Fn random 6420578Smckusickwill by default produce a sequence of numbers that can be duplicated 6520578Smckusickby calling 6648349Scael.Fn srandom 6720578Smckusickwith 6848349Scael.Ql 1 6920578Smckusickas the seed. 7048349Scael.Pp 7120578SmckusickThe 7248349Scael.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 7548349Scael.Fn initstate 7648349Scaelto 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 8048349Scaelthe 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. 8448349ScaelThe 8548349Scael.Fn initstate 8648349Scaelfunction 8720578Smckusickreturns a pointer to the previous state information array. 8848349Scael.Pp 8920578SmckusickOnce a state has been initialized, the 9048349Scael.Fn setstate 9120578Smckusickroutine provides for rapid switching between states. 9248349ScaelThe 9348349Scael.Fn setstate 9448349Scaelfunction 9520579Smckusickreturns a pointer to the previous state array; its 9620578Smckusickargument state array is used for further random number generation 9720578Smckusickuntil the next call to 9848349Scael.Fn initstate 9920578Smckusickor 10048349Scael.Fn setstate . 10148349Scael.Pp 10220578SmckusickOnce a state array has been initialized, it may be restarted at a 10320578Smckusickdifferent point either by calling 10448349Scael.Fn initstate 10520578Smckusick(with the desired seed, the state array, and its size) or by calling 10620578Smckusickboth 10748349Scael.Fn setstate 10820578Smckusick(with the state array) and 10948349Scael.Fn srandom 11020578Smckusick(with the desired seed). 11120578SmckusickThe advantage of calling both 11248349Scael.Fn setstate 11320578Smckusickand 11448349Scael.Fn srandom 11520578Smckusickis that the size of the state array does not have to be remembered after 11620578Smckusickit is initialized. 11748349Scael.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. 12348349Scael.Sh AUTHOR 12420578SmckusickEarl T. Cohen 12548349Scael.Sh DIAGNOSTICS 12620578SmckusickIf 12748349Scael.Fn initstate 12820578Smckusickis called with less than 8 bytes of state information, or if 12948349Scael.Fn setstate 13020578Smckusickdetects that the state information has been garbled, error 13120578Smckusickmessages are printed on the standard error output. 13248349Scael.Sh SEE ALSO 13348349Scael.Xr rand 3 13448349Scael.Sh HISTORY 13548349ScaelThese 13648349Scaelfunctions appeared in 13748349Scael.Bx 4.2 . 13848349Scael.Sh BUGS 13920578SmckusickAbout 2/3 the speed of 14048349Scael.Xr rand 3 . 141