158f0484fSRodney W. Grimes.\" Copyright (c) 1983, 1991, 1993 258f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 358f0484fSRodney W. Grimes.\" 458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 658f0484fSRodney W. Grimes.\" are met: 758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 858f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 1058f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 1158f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 1258f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software 1358f0484fSRodney W. Grimes.\" must display the following acknowledgement: 1458f0484fSRodney W. Grimes.\" This product includes software developed by the University of 1558f0484fSRodney W. Grimes.\" California, Berkeley and its contributors. 1658f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors 1758f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1858f0484fSRodney W. Grimes.\" without specific prior written permission. 1958f0484fSRodney W. Grimes.\" 2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3058f0484fSRodney W. Grimes.\" SUCH DAMAGE. 3158f0484fSRodney W. Grimes.\" 3258f0484fSRodney W. Grimes.\" @(#)random.3 8.1 (Berkeley) 6/4/93 337f3dea24SPeter Wemm.\" $FreeBSD$ 3458f0484fSRodney W. Grimes.\" 3558f0484fSRodney W. Grimes.Dd June 4, 1993 3658f0484fSRodney W. Grimes.Dt RANDOM 3 37a307d598SRuslan Ermilov.Os 3858f0484fSRodney W. Grimes.Sh NAME 3958f0484fSRodney W. Grimes.Nm random , 4058f0484fSRodney W. Grimes.Nm srandom , 417b0b1b2fSAndrey A. Chernov.Nm srandomdev , 4258f0484fSRodney W. Grimes.Nm initstate , 4358f0484fSRodney W. Grimes.Nm setstate 4458f0484fSRodney W. Grimes.Nd better random number generator; routines for changing generators 4525bb73e0SAlexey Zelkin.Sh LIBRARY 4625bb73e0SAlexey Zelkin.Lb libc 4758f0484fSRodney W. Grimes.Sh SYNOPSIS 488aefde06SJeroen Ruigrok van der Werven.In stdlib.h 4958f0484fSRodney W. Grimes.Ft long 5058f0484fSRodney W. Grimes.Fn random void 5158f0484fSRodney W. Grimes.Ft void 5294fa7afdSAndrey A. Chernov.Fn srandom "unsigned long seed" 5396c31b26SAndrey A. Chernov.Ft void 547b0b1b2fSAndrey A. Chernov.Fn srandomdev void 5558f0484fSRodney W. Grimes.Ft char * 5694fa7afdSAndrey A. Chernov.Fn initstate "unsigned long seed" "char *state" "long n" 5758f0484fSRodney W. Grimes.Ft char * 5858f0484fSRodney W. Grimes.Fn setstate "char *state" 5958f0484fSRodney W. Grimes.Sh DESCRIPTION 6058f0484fSRodney W. GrimesThe 6158f0484fSRodney W. Grimes.Fn random 6258f0484fSRodney W. Grimesfunction 6358f0484fSRodney W. Grimesuses a non-linear additive feedback random number generator employing a 6458f0484fSRodney W. Grimesdefault table of size 31 long integers to return successive pseudo-random 6558f0484fSRodney W. Grimesnumbers in the range from 0 to 6658f0484fSRodney W. Grimes.if t 2\u\s731\s10\d\(mi1. 6758f0484fSRodney W. Grimes.if n (2**31)\(mi1. 6858f0484fSRodney W. GrimesThe period of this random number generator is very large, approximately 6958f0484fSRodney W. Grimes.if t 16\(mu(2\u\s731\s10\d\(mi1). 7058f0484fSRodney W. Grimes.if n 16*((2**31)\(mi1). 7158f0484fSRodney W. Grimes.Pp 7258f0484fSRodney W. GrimesThe 73064f0074SMike Pritchard.Fn random 74064f0074SMike Pritchardand 75064f0074SMike Pritchard.Fn srandom 76064f0074SMike Pritchardfunctions have (almost) the same calling sequence and initialization properties as the 77064f0074SMike Pritchard.Xr rand 3 78064f0074SMike Pritchardand 79064f0074SMike Pritchard.Xr srand 3 80064f0074SMike Pritchardfunctions. 8158f0484fSRodney W. GrimesThe difference is that 82064f0074SMike Pritchard.Xr rand 3 8358f0484fSRodney W. Grimesproduces a much less random sequence \(em in fact, the low dozen bits 84*1a0a9345SRuslan Ermilovgenerated by rand go through a cyclic pattern. 85*1a0a9345SRuslan ErmilovAll the bits generated by 8658f0484fSRodney W. Grimes.Fn random 87*1a0a9345SRuslan Ermilovare usable. 88*1a0a9345SRuslan ErmilovFor example, 8958f0484fSRodney W. Grimes.Sq Li random()&01 9058f0484fSRodney W. Grimeswill produce a random binary 9158f0484fSRodney W. Grimesvalue. 9258f0484fSRodney W. Grimes.Pp 9340f8b70dSAndrey A. ChernovLike 9458f0484fSRodney W. Grimes.Xr rand 3 , 9558f0484fSRodney W. Grimes.Fn random 9658f0484fSRodney W. Grimeswill by default produce a sequence of numbers that can be duplicated 9758f0484fSRodney W. Grimesby calling 9858f0484fSRodney W. Grimes.Fn srandom 9958f0484fSRodney W. Grimeswith 10058f0484fSRodney W. Grimes.Ql 1 10158f0484fSRodney W. Grimesas the seed. 10258f0484fSRodney W. Grimes.Pp 10358f0484fSRodney W. GrimesThe 1047b0b1b2fSAndrey A. Chernov.Fn srandomdev 105f8f69700SJens Schweikhardtroutine initializes a state array using the 106df49b8e2SDima Dorfman.Xr random 4 1077b0b1b2fSAndrey A. Chernovrandom number device which returns good random numbers, 1087b0b1b2fSAndrey A. Chernovsuitable for cryptographic use. 1097b0b1b2fSAndrey A. ChernovNote that this particular seeding 1107b0b1b2fSAndrey A. Chernovprocedure can generate states which are impossible to reproduce by 1117b0b1b2fSAndrey A. Chernovcalling 1127b0b1b2fSAndrey A. Chernov.Fn srandom 1137b0b1b2fSAndrey A. Chernovwith any value, since the succeeding terms in the 1147b0b1b2fSAndrey A. Chernovstate buffer are no longer derived from the LC algorithm applied to 1157b0b1b2fSAndrey A. Chernova fixed seed. 1167b0b1b2fSAndrey A. Chernov.Pp 1177b0b1b2fSAndrey A. ChernovThe 11858f0484fSRodney W. Grimes.Fn initstate 11958f0484fSRodney W. Grimesroutine allows a state array, passed in as an argument, to be initialized 120*1a0a9345SRuslan Ermilovfor future use. 121*1a0a9345SRuslan ErmilovThe size of the state array (in bytes) is used by 12258f0484fSRodney W. Grimes.Fn initstate 12358f0484fSRodney W. Grimesto decide how sophisticated a random number generator it should use \(em the 12458f0484fSRodney W. Grimesmore state, the better the random numbers will be. 12558f0484fSRodney W. Grimes(Current "optimal" values for the amount of state information are 12658f0484fSRodney W. Grimes8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to 127*1a0a9345SRuslan Ermilovthe nearest known amount. 128*1a0a9345SRuslan ErmilovUsing less than 8 bytes will cause an error.) 12958f0484fSRodney W. GrimesThe seed for the initialization (which specifies a starting point for 13058f0484fSRodney W. Grimesthe random number sequence, and provides for restarting at the same 13158f0484fSRodney W. Grimespoint) is also an argument. 13258f0484fSRodney W. GrimesThe 13358f0484fSRodney W. Grimes.Fn initstate 13458f0484fSRodney W. Grimesfunction 13558f0484fSRodney W. Grimesreturns a pointer to the previous state information array. 13658f0484fSRodney W. Grimes.Pp 13758f0484fSRodney W. GrimesOnce a state has been initialized, the 13858f0484fSRodney W. Grimes.Fn setstate 13958f0484fSRodney W. Grimesroutine provides for rapid switching between states. 14058f0484fSRodney W. GrimesThe 14158f0484fSRodney W. Grimes.Fn setstate 14258f0484fSRodney W. Grimesfunction 14358f0484fSRodney W. Grimesreturns a pointer to the previous state array; its 14458f0484fSRodney W. Grimesargument state array is used for further random number generation 14558f0484fSRodney W. Grimesuntil the next call to 14658f0484fSRodney W. Grimes.Fn initstate 14758f0484fSRodney W. Grimesor 14858f0484fSRodney W. Grimes.Fn setstate . 14958f0484fSRodney W. Grimes.Pp 15058f0484fSRodney W. GrimesOnce a state array has been initialized, it may be restarted at a 15158f0484fSRodney W. Grimesdifferent point either by calling 15258f0484fSRodney W. Grimes.Fn initstate 15358f0484fSRodney W. Grimes(with the desired seed, the state array, and its size) or by calling 15458f0484fSRodney W. Grimesboth 15558f0484fSRodney W. Grimes.Fn setstate 15658f0484fSRodney W. Grimes(with the state array) and 15758f0484fSRodney W. Grimes.Fn srandom 15858f0484fSRodney W. Grimes(with the desired seed). 15958f0484fSRodney W. GrimesThe advantage of calling both 16058f0484fSRodney W. Grimes.Fn setstate 16158f0484fSRodney W. Grimesand 16258f0484fSRodney W. Grimes.Fn srandom 16358f0484fSRodney W. Grimesis that the size of the state array does not have to be remembered after 16458f0484fSRodney W. Grimesit is initialized. 16558f0484fSRodney W. Grimes.Pp 16658f0484fSRodney W. GrimesWith 256 bytes of state information, the period of the random number 16758f0484fSRodney W. Grimesgenerator is greater than 16858f0484fSRodney W. Grimes.if t 2\u\s769\s10\d, 16958f0484fSRodney W. Grimes.if n 2**69 17058f0484fSRodney W. Grimeswhich should be sufficient for most purposes. 171a5941fc2SPhilippe Charnier.Sh AUTHORS 172a5941fc2SPhilippe Charnier.An Earl T. Cohen 17358f0484fSRodney W. Grimes.Sh DIAGNOSTICS 17458f0484fSRodney W. GrimesIf 17558f0484fSRodney W. Grimes.Fn initstate 17658f0484fSRodney W. Grimesis called with less than 8 bytes of state information, or if 17758f0484fSRodney W. Grimes.Fn setstate 17858f0484fSRodney W. Grimesdetects that the state information has been garbled, error 17958f0484fSRodney W. Grimesmessages are printed on the standard error output. 18058f0484fSRodney W. Grimes.Sh SEE ALSO 181e02cc8e4SDavid Schultz.Xr arc4random 3 , 18240f8b70dSAndrey A. Chernov.Xr rand 3 , 1837b0b1b2fSAndrey A. Chernov.Xr srand 3 , 184df49b8e2SDima Dorfman.Xr random 4 18558f0484fSRodney W. Grimes.Sh HISTORY 18658f0484fSRodney W. GrimesThese 18758f0484fSRodney W. Grimesfunctions appeared in 18858f0484fSRodney W. Grimes.Bx 4.2 . 18958f0484fSRodney W. Grimes.Sh BUGS 19058f0484fSRodney W. GrimesAbout 2/3 the speed of 19158f0484fSRodney W. Grimes.Xr rand 3 . 19240f8b70dSAndrey A. Chernov.Pp 19340f8b70dSAndrey A. ChernovThe historical implementation used to have a very weak seeding; the 194a080f578SAndrey A. Chernovrandom sequence did not vary much with the seed. 19540f8b70dSAndrey A. ChernovThe current implementation employs a better pseudo-random number 19640f8b70dSAndrey A. Chernovgenerator for the initial state calculation. 197e02cc8e4SDavid Schultz.Pp 198e02cc8e4SDavid SchultzApplications requiring cryptographic quality randomness should use 199e02cc8e4SDavid Schultz.Xr arc4random 3 . 200