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.\" $FreeBSD$ 34.\" 35.Dd June 4, 1993 36.Dt RANDOM 3 37.Os 38.Sh NAME 39.Nm random , 40.Nm srandom , 41.Nm srandomdev , 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.In stdlib.h 49.Ft long 50.Fn random void 51.Ft void 52.Fn srandom "unsigned long seed" 53.Ft void 54.Fn srandomdev void 55.Ft char * 56.Fn initstate "unsigned long seed" "char *state" "long n" 57.Ft char * 58.Fn setstate "char *state" 59.Sh DESCRIPTION 60The 61.Fn random 62function 63uses a non-linear additive feedback random number generator employing a 64default table of size 31 long integers to return successive pseudo-random 65numbers in the range from 0 to 66.if t 2\u\s731\s10\d\(mi1. 67.if n (2**31)\(mi1. 68The period of this random number generator is very large, approximately 69.if t 16\(mu(2\u\s731\s10\d\(mi1). 70.if n 16*((2**31)\(mi1). 71.Pp 72The 73.Fn random 74and 75.Fn srandom 76functions have (almost) the same calling sequence and initialization properties as the 77.Xr rand 3 78and 79.Xr srand 3 80functions. 81The difference is that 82.Xr rand 3 83produces a much less random sequence \(em in fact, the low dozen bits 84generated by rand go through a cyclic pattern. 85All the bits generated by 86.Fn random 87are usable. 88For example, 89.Sq Li random()&01 90will produce a random binary 91value. 92.Pp 93Like 94.Xr rand 3 , 95.Fn random 96will by default produce a sequence of numbers that can be duplicated 97by calling 98.Fn srandom 99with 100.Ql 1 101as the seed. 102.Pp 103The 104.Fn srandomdev 105routine initializes a state array using the 106.Xr random 4 107random number device which returns good random numbers, 108suitable for cryptographic use. 109Note that this particular seeding 110procedure can generate states which are impossible to reproduce by 111calling 112.Fn srandom 113with any value, since the succeeding terms in the 114state buffer are no longer derived from the LC algorithm applied to 115a fixed seed. 116.Pp 117The 118.Fn initstate 119routine allows a state array, passed in as an argument, to be initialized 120for future use. 121The size of the state array (in bytes) is used by 122.Fn initstate 123to decide how sophisticated a random number generator it should use \(em the 124more state, the better the random numbers will be. 125(Current "optimal" values for the amount of state information are 1268, 32, 64, 128, and 256 bytes; other amounts will be rounded down to 127the nearest known amount. 128Using less than 8 bytes will cause an error.) 129The seed for the initialization (which specifies a starting point for 130the random number sequence, and provides for restarting at the same 131point) is also an argument. 132The 133.Fn initstate 134function 135returns a pointer to the previous state information array. 136.Pp 137Once a state has been initialized, the 138.Fn setstate 139routine provides for rapid switching between states. 140The 141.Fn setstate 142function 143returns a pointer to the previous state array; its 144argument state array is used for further random number generation 145until the next call to 146.Fn initstate 147or 148.Fn setstate . 149.Pp 150Once a state array has been initialized, it may be restarted at a 151different point either by calling 152.Fn initstate 153(with the desired seed, the state array, and its size) or by calling 154both 155.Fn setstate 156(with the state array) and 157.Fn srandom 158(with the desired seed). 159The advantage of calling both 160.Fn setstate 161and 162.Fn srandom 163is that the size of the state array does not have to be remembered after 164it is initialized. 165.Pp 166With 256 bytes of state information, the period of the random number 167generator is greater than 168.if t 2\u\s769\s10\d, 169.if n 2**69 170which should be sufficient for most purposes. 171.Sh AUTHORS 172.An Earl T. Cohen 173.Sh DIAGNOSTICS 174If 175.Fn initstate 176is called with less than 8 bytes of state information, or if 177.Fn setstate 178detects that the state information has been garbled, error 179messages are printed on the standard error output. 180.Sh SEE ALSO 181.Xr arc4random 3 , 182.Xr rand 3 , 183.Xr srand 3 , 184.Xr random 4 185.Sh HISTORY 186These 187functions appeared in 188.Bx 4.2 . 189.Sh BUGS 190About 2/3 the speed of 191.Xr rand 3 . 192.Pp 193The historical implementation used to have a very weak seeding; the 194random sequence did not vary much with the seed. 195The current implementation employs a better pseudo-random number 196generator for the initial state calculation. 197.Pp 198Applications requiring cryptographic quality randomness should use 199.Xr arc4random 3 . 200