xref: /minix3/lib/libc/stdlib/random.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\"	$NetBSD: random.3,v 1.21 2014/06/12 18:46:12 christos Exp $
22fe8fb19SBen Gras.\"
32fe8fb19SBen Gras.\" Copyright (c) 1983, 1991, 1993
42fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras.\"
62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
82fe8fb19SBen Gras.\" are met:
92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras.\"    without specific prior written permission.
172fe8fb19SBen Gras.\"
182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras.\" SUCH DAMAGE.
292fe8fb19SBen Gras.\"
302fe8fb19SBen Gras.\"     from: @(#)random.3	8.1 (Berkeley) 6/4/93
312fe8fb19SBen Gras.\"
32*0a6a1f1dSLionel Sambuc.Dd June 12, 2014
332fe8fb19SBen Gras.Dt RANDOM 3
342fe8fb19SBen Gras.Os
352fe8fb19SBen Gras.Sh NAME
362fe8fb19SBen Gras.Nm random ,
372fe8fb19SBen Gras.Nm srandom ,
382fe8fb19SBen Gras.Nm initstate ,
392fe8fb19SBen Gras.Nm setstate
402fe8fb19SBen Gras.Nd better random number generator; routines for changing generators
412fe8fb19SBen Gras.Sh LIBRARY
422fe8fb19SBen Gras.Lb libc
432fe8fb19SBen Gras.Sh SYNOPSIS
442fe8fb19SBen Gras.In stdlib.h
452fe8fb19SBen Gras.Ft long
462fe8fb19SBen Gras.Fn random void
472fe8fb19SBen Gras.Ft void
48*0a6a1f1dSLionel Sambuc.Fn srandom "unsigned int seed"
492fe8fb19SBen Gras.Ft char *
50*0a6a1f1dSLionel Sambuc.Fn initstate "unsigned int seed" "char *state" "size_t n"
512fe8fb19SBen Gras.Ft char *
522fe8fb19SBen Gras.Fn setstate "char *state"
532fe8fb19SBen Gras.Sh DESCRIPTION
542fe8fb19SBen GrasThe
552fe8fb19SBen Gras.Fn random
562fe8fb19SBen Grasfunction
572fe8fb19SBen Grasuses a non-linear additive feedback random number generator employing a
582fe8fb19SBen Grasdefault table of size 31 long integers to return successive pseudo-random
592fe8fb19SBen Grasnumbers in the range from 0 to
602fe8fb19SBen Gras.if t 2\u\s731\s10\d\(mi1.
612fe8fb19SBen Gras.if n (2**31)\(mi1.
622fe8fb19SBen GrasThe period of this random number generator is very large, approximately
632fe8fb19SBen Gras.if t 16\(mu(2\u\s731\s10\d\(mi1).
642fe8fb19SBen Gras.if n 16*((2**31)\(mi1).
652fe8fb19SBen GrasThe maximum value
662fe8fb19SBen Gras.Dv RANDOM_MAX
672fe8fb19SBen Grasis defined in
682fe8fb19SBen Gras.In stdlib.h .
692fe8fb19SBen Gras.Pp
702fe8fb19SBen GrasThe
712fe8fb19SBen Gras.Fn random
722fe8fb19SBen Grasand
732fe8fb19SBen Gras.Fn srandom
742fe8fb19SBen Grashave (almost) the same calling sequence and initialization properties as
752fe8fb19SBen Gras.Xr rand 3
762fe8fb19SBen Grasand
772fe8fb19SBen Gras.Xr srand 3 .
782fe8fb19SBen GrasThe difference is that
792fe8fb19SBen Gras.Xr rand 3
802fe8fb19SBen Grasproduces a much less random sequence \(em in fact, the low dozen bits
812fe8fb19SBen Grasgenerated by
822fe8fb19SBen Gras.Xr rand 3
832fe8fb19SBen Grasgo through a cyclic pattern.
842fe8fb19SBen GrasAll the bits generated by
852fe8fb19SBen Gras.Fn random
862fe8fb19SBen Grasare usable.
872fe8fb19SBen GrasFor example,
882fe8fb19SBen Gras.Sq Li random()\*[Am]01
892fe8fb19SBen Graswill produce a random binary value.
902fe8fb19SBen Gras.Pp
912fe8fb19SBen GrasLike
922fe8fb19SBen Gras.Xr rand 3 ,
932fe8fb19SBen Gras.Fn random
942fe8fb19SBen Graswill by default produce a sequence of numbers that can be duplicated
952fe8fb19SBen Grasby calling
962fe8fb19SBen Gras.Fn srandom
972fe8fb19SBen Graswith
982fe8fb19SBen Gras.Ql 1
992fe8fb19SBen Grasas the seed.
1002fe8fb19SBen Gras.Pp
1012fe8fb19SBen GrasThe
1022fe8fb19SBen Gras.Fn initstate
1032fe8fb19SBen Grasroutine allows a state array, passed in as an argument, to be initialized
1042fe8fb19SBen Grasfor future use.
1052fe8fb19SBen GrasThe size of the state array (in bytes) is used by
1062fe8fb19SBen Gras.Fn initstate
1072fe8fb19SBen Grasto decide how sophisticated a random number generator it should use \(em the
1082fe8fb19SBen Grasmore state, the better the random numbers will be.
1092fe8fb19SBen Gras(Current "optimal" values for the amount of state information are
1102fe8fb19SBen Gras8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
1112fe8fb19SBen Grasthe nearest known amount.
1122fe8fb19SBen GrasUsing less than 8 bytes will cause an error).
1132fe8fb19SBen GrasThe seed for the initialization (which specifies a starting point for
1142fe8fb19SBen Grasthe random number sequence, and provides for restarting at the same
1152fe8fb19SBen Graspoint) is also an argument.
1162fe8fb19SBen GrasThe state array passed to
1172fe8fb19SBen Gras.Fn initstate
1182fe8fb19SBen Grasmust be aligned to a 32-bit boundary.
1192fe8fb19SBen GrasThis can be achieved by using
1202fe8fb19SBen Grasa suitably-sized array of ints, and casting the array to char * when
1212fe8fb19SBen Graspassing it to
1222fe8fb19SBen Gras.Fn initstate .
1232fe8fb19SBen GrasThe
1242fe8fb19SBen Gras.Fn initstate
1252fe8fb19SBen Grasfunction
1262fe8fb19SBen Grasreturns a pointer to the previous state information array.
1272fe8fb19SBen Gras.Pp
1282fe8fb19SBen GrasOnce a state has been initialized, the
1292fe8fb19SBen Gras.Fn setstate
1302fe8fb19SBen Grasroutine provides for rapid switching between states.
1312fe8fb19SBen GrasThe
1322fe8fb19SBen Gras.Fn setstate
1332fe8fb19SBen Grasfunction
1342fe8fb19SBen Grasreturns a pointer to the previous state array; its
1352fe8fb19SBen Grasargument state array is used for further random number generation
1362fe8fb19SBen Grasuntil the next call to
1372fe8fb19SBen Gras.Fn initstate
1382fe8fb19SBen Grasor
1392fe8fb19SBen Gras.Fn setstate .
1402fe8fb19SBen Gras.Pp
1412fe8fb19SBen GrasOnce a state array has been initialized, it may be restarted at a
1422fe8fb19SBen Grasdifferent point either by calling
1432fe8fb19SBen Gras.Fn initstate
1442fe8fb19SBen Gras(with the desired seed, the state array, and its size) or by calling
1452fe8fb19SBen Grasboth
1462fe8fb19SBen Gras.Fn setstate
1472fe8fb19SBen Gras(with the state array) and
1482fe8fb19SBen Gras.Fn srandom
1492fe8fb19SBen Gras(with the desired seed).
1502fe8fb19SBen GrasThe advantage of calling both
1512fe8fb19SBen Gras.Fn setstate
1522fe8fb19SBen Grasand
1532fe8fb19SBen Gras.Fn srandom
1542fe8fb19SBen Grasis that the size of the state array does not have to be remembered after
1552fe8fb19SBen Grasit is initialized.
1562fe8fb19SBen Gras.Pp
1572fe8fb19SBen GrasWith 256 bytes of state information, the period of the random number
1582fe8fb19SBen Grasgenerator is greater than
1592fe8fb19SBen Gras.if t 2\u\s769\s10\d,
1602fe8fb19SBen Gras.if n 2**69
1612fe8fb19SBen Graswhich should be sufficient for most purposes.
1622fe8fb19SBen Gras.Sh DIAGNOSTICS
1632fe8fb19SBen GrasIf
1642fe8fb19SBen Gras.Fn initstate
1652fe8fb19SBen Grasis called with less than 8 bytes of state information, or if
1662fe8fb19SBen Gras.Fn setstate
1672fe8fb19SBen Grasdetects that the state information has been garbled, error
1682fe8fb19SBen Grasmessages are printed on the standard error output.
1692fe8fb19SBen Gras.Sh SEE ALSO
1702fe8fb19SBen Gras.Xr rand 3 ,
1712fe8fb19SBen Gras.Xr srand 3 ,
1722fe8fb19SBen Gras.Xr rnd 4 ,
1732fe8fb19SBen Gras.Xr rnd 9
174f14fb602SLionel Sambuc.Sh STANDARDS
175f14fb602SLionel SambucThe
176f14fb602SLionel Sambuc.Fn random ,
177f14fb602SLionel Sambuc.Fn srandom ,
178f14fb602SLionel Sambuc.Fn initstate
179f14fb602SLionel Sambucand
180f14fb602SLionel Sambuc.Fn setstate
181f14fb602SLionel Sambucfunctions conform to
182f14fb602SLionel Sambuc.St -p1003.1-2008 .
1832fe8fb19SBen Gras.Sh HISTORY
1842fe8fb19SBen GrasThese
1852fe8fb19SBen Grasfunctions appeared in
1862fe8fb19SBen Gras.Bx 4.2 .
1872fe8fb19SBen Gras.Sh AUTHORS
1882fe8fb19SBen Gras.An Earl T. Cohen
1892fe8fb19SBen Gras.Sh BUGS
1902fe8fb19SBen GrasAbout 2/3 the speed of
1912fe8fb19SBen Gras.Xr rand 3 .
192