xref: /freebsd-14.2/lib/libc/stdlib/random.3 (revision 064f0074)
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
3358f0484fSRodney W. Grimes.\"
3458f0484fSRodney W. Grimes.Dd June 4, 1993
3558f0484fSRodney W. Grimes.Dt RANDOM 3
3658f0484fSRodney W. Grimes.Os BSD 4.2
3758f0484fSRodney W. Grimes.Sh NAME
3858f0484fSRodney W. Grimes.Nm random ,
3958f0484fSRodney W. Grimes.Nm srandom ,
4058f0484fSRodney W. Grimes.Nm initstate ,
4158f0484fSRodney W. Grimes.Nm setstate
4258f0484fSRodney W. Grimes.Nd better random number generator; routines for changing generators
4358f0484fSRodney W. Grimes.Sh SYNOPSIS
4458f0484fSRodney W. Grimes.Fd #include <stdlib.h>
4558f0484fSRodney W. Grimes.Ft long
4658f0484fSRodney W. Grimes.Fn random void
4758f0484fSRodney W. Grimes.Ft void
4858f0484fSRodney W. Grimes.Fn srandom "unsigned seed"
4958f0484fSRodney W. Grimes.Ft char *
5058f0484fSRodney W. Grimes.Fn initstate "unsigned seed" "char *state" "int n"
5158f0484fSRodney W. Grimes.Ft char *
5258f0484fSRodney W. Grimes.Fn setstate "char *state"
5358f0484fSRodney W. Grimes.Sh DESCRIPTION
5458f0484fSRodney W. GrimesThe
5558f0484fSRodney W. Grimes.Fn random
5658f0484fSRodney W. Grimesfunction
5758f0484fSRodney W. Grimesuses a non-linear additive feedback random number generator employing a
5858f0484fSRodney W. Grimesdefault table of size 31 long integers to return successive pseudo-random
5958f0484fSRodney W. Grimesnumbers in the range from 0 to
6058f0484fSRodney W. Grimes.if t 2\u\s731\s10\d\(mi1.
6158f0484fSRodney W. Grimes.if n (2**31)\(mi1.
6258f0484fSRodney W. GrimesThe period of this random number generator is very large, approximately
6358f0484fSRodney W. Grimes.if t 16\(mu(2\u\s731\s10\d\(mi1).
6458f0484fSRodney W. Grimes.if n 16*((2**31)\(mi1).
6558f0484fSRodney W. Grimes.Pp
6658f0484fSRodney W. GrimesThe
67*064f0074SMike Pritchard.Fn random
68*064f0074SMike Pritchardand
69*064f0074SMike Pritchard.Fn srandom
70*064f0074SMike Pritchardfunctions have (almost) the same calling sequence and initialization properties as the
71*064f0074SMike Pritchard.Xr rand 3
72*064f0074SMike Pritchardand
73*064f0074SMike Pritchard.Xr srand 3
74*064f0074SMike Pritchardfunctions.
7558f0484fSRodney W. GrimesThe difference is that
76*064f0074SMike Pritchard.Xr rand 3
7758f0484fSRodney W. Grimesproduces a much less random sequence \(em in fact, the low dozen bits
7858f0484fSRodney W. Grimesgenerated by rand go through a cyclic pattern.  All the bits generated by
7958f0484fSRodney W. Grimes.Fn random
8058f0484fSRodney W. Grimesare usable.  For example,
8158f0484fSRodney W. Grimes.Sq Li random()&01
8258f0484fSRodney W. Grimeswill produce a random binary
8358f0484fSRodney W. Grimesvalue.
8458f0484fSRodney W. Grimes.Pp
8558f0484fSRodney W. GrimesUnlike
8658f0484fSRodney W. Grimes.Xr srand ,
8758f0484fSRodney W. Grimes.Fn srandom
8858f0484fSRodney W. Grimesdoes not return the old seed; the reason for this is that the amount of
8958f0484fSRodney W. Grimesstate information used is much more than a single word.  (Two other
9058f0484fSRodney W. Grimesroutines are provided to deal with restarting/changing random
9158f0484fSRodney W. Grimesnumber generators).  Like
9258f0484fSRodney W. Grimes.Xr rand 3 ,
9358f0484fSRodney W. Grimeshowever,
9458f0484fSRodney W. Grimes.Fn random
9558f0484fSRodney W. Grimeswill by default produce a sequence of numbers that can be duplicated
9658f0484fSRodney W. Grimesby calling
9758f0484fSRodney W. Grimes.Fn srandom
9858f0484fSRodney W. Grimeswith
9958f0484fSRodney W. Grimes.Ql 1
10058f0484fSRodney W. Grimesas the seed.
10158f0484fSRodney W. Grimes.Pp
10258f0484fSRodney W. GrimesThe
10358f0484fSRodney W. Grimes.Fn initstate
10458f0484fSRodney W. Grimesroutine allows a state array, passed in as an argument, to be initialized
10558f0484fSRodney W. Grimesfor future use.  The size of the state array (in bytes) is used by
10658f0484fSRodney W. Grimes.Fn initstate
10758f0484fSRodney W. Grimesto decide how sophisticated a random number generator it should use \(em the
10858f0484fSRodney W. Grimesmore state, the better the random numbers will be.
10958f0484fSRodney W. Grimes(Current "optimal" values for the amount of state information are
11058f0484fSRodney W. Grimes8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
11158f0484fSRodney W. Grimesthe nearest known amount.  Using less than 8 bytes will cause an error.)
11258f0484fSRodney W. GrimesThe seed for the initialization (which specifies a starting point for
11358f0484fSRodney W. Grimesthe random number sequence, and provides for restarting at the same
11458f0484fSRodney W. Grimespoint) is also an argument.
11558f0484fSRodney W. GrimesThe
11658f0484fSRodney W. Grimes.Fn initstate
11758f0484fSRodney W. Grimesfunction
11858f0484fSRodney W. Grimesreturns a pointer to the previous state information array.
11958f0484fSRodney W. Grimes.Pp
12058f0484fSRodney W. GrimesOnce a state has been initialized, the
12158f0484fSRodney W. Grimes.Fn setstate
12258f0484fSRodney W. Grimesroutine provides for rapid switching between states.
12358f0484fSRodney W. GrimesThe
12458f0484fSRodney W. Grimes.Fn setstate
12558f0484fSRodney W. Grimesfunction
12658f0484fSRodney W. Grimesreturns a pointer to the previous state array; its
12758f0484fSRodney W. Grimesargument state array is used for further random number generation
12858f0484fSRodney W. Grimesuntil the next call to
12958f0484fSRodney W. Grimes.Fn initstate
13058f0484fSRodney W. Grimesor
13158f0484fSRodney W. Grimes.Fn setstate .
13258f0484fSRodney W. Grimes.Pp
13358f0484fSRodney W. GrimesOnce a state array has been initialized, it may be restarted at a
13458f0484fSRodney W. Grimesdifferent point either by calling
13558f0484fSRodney W. Grimes.Fn initstate
13658f0484fSRodney W. Grimes(with the desired seed, the state array, and its size) or by calling
13758f0484fSRodney W. Grimesboth
13858f0484fSRodney W. Grimes.Fn setstate
13958f0484fSRodney W. Grimes(with the state array) and
14058f0484fSRodney W. Grimes.Fn srandom
14158f0484fSRodney W. Grimes(with the desired seed).
14258f0484fSRodney W. GrimesThe advantage of calling both
14358f0484fSRodney W. Grimes.Fn setstate
14458f0484fSRodney W. Grimesand
14558f0484fSRodney W. Grimes.Fn srandom
14658f0484fSRodney W. Grimesis that the size of the state array does not have to be remembered after
14758f0484fSRodney W. Grimesit is initialized.
14858f0484fSRodney W. Grimes.Pp
14958f0484fSRodney W. GrimesWith 256 bytes of state information, the period of the random number
15058f0484fSRodney W. Grimesgenerator is greater than
15158f0484fSRodney W. Grimes.if t 2\u\s769\s10\d,
15258f0484fSRodney W. Grimes.if n 2**69
15358f0484fSRodney W. Grimeswhich should be sufficient for most purposes.
15458f0484fSRodney W. Grimes.Sh AUTHOR
15558f0484fSRodney W. GrimesEarl T. Cohen
15658f0484fSRodney W. Grimes.Sh DIAGNOSTICS
15758f0484fSRodney W. GrimesIf
15858f0484fSRodney W. Grimes.Fn initstate
15958f0484fSRodney W. Grimesis called with less than 8 bytes of state information, or if
16058f0484fSRodney W. Grimes.Fn setstate
16158f0484fSRodney W. Grimesdetects that the state information has been garbled, error
16258f0484fSRodney W. Grimesmessages are printed on the standard error output.
16358f0484fSRodney W. Grimes.Sh SEE ALSO
16458f0484fSRodney W. Grimes.Xr rand 3
16558f0484fSRodney W. Grimes.Sh HISTORY
16658f0484fSRodney W. GrimesThese
16758f0484fSRodney W. Grimesfunctions appeared in
16858f0484fSRodney W. Grimes.Bx 4.2 .
16958f0484fSRodney W. Grimes.Sh BUGS
17058f0484fSRodney W. GrimesAbout 2/3 the speed of
17158f0484fSRodney W. Grimes.Xr rand 3 .
172