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