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