xref: /freebsd-14.2/lib/libc/stdlib/random.3 (revision b2c76c41)
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.
12580b4d18SEd Maste.\" 3. Neither the name of the University nor the names of its contributors
1358f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1458f0484fSRodney W. Grimes.\"    without specific prior written permission.
1558f0484fSRodney W. Grimes.\"
1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2658f0484fSRodney W. Grimes.\" SUCH DAMAGE.
2758f0484fSRodney W. Grimes.\"
2858f0484fSRodney W. Grimes.\"     @(#)random.3	8.1 (Berkeley) 6/4/93
2958f0484fSRodney W. Grimes.\"
30*672e1225SConrad Meyer.Dd February 1, 2020
3158f0484fSRodney W. Grimes.Dt RANDOM 3
32a307d598SRuslan Ermilov.Os
3358f0484fSRodney W. Grimes.Sh NAME
3458f0484fSRodney W. Grimes.Nm random ,
3558f0484fSRodney W. Grimes.Nm srandom ,
367b0b1b2fSAndrey A. Chernov.Nm srandomdev ,
3758f0484fSRodney W. Grimes.Nm initstate ,
3858f0484fSRodney W. Grimes.Nm setstate
391631235aSConrad Meyer.Nd non-cryptographic pseudorandom number generator; routines for changing generators
4025bb73e0SAlexey Zelkin.Sh LIBRARY
4125bb73e0SAlexey Zelkin.Lb libc
4258f0484fSRodney W. Grimes.Sh SYNOPSIS
438aefde06SJeroen Ruigrok van der Werven.In stdlib.h
4458f0484fSRodney W. Grimes.Ft long
4558f0484fSRodney W. Grimes.Fn random void
4658f0484fSRodney W. Grimes.Ft void
478de6c267SEd Schouten.Fn srandom "unsigned int seed"
4896c31b26SAndrey A. Chernov.Ft void
497b0b1b2fSAndrey A. Chernov.Fn srandomdev void
5058f0484fSRodney W. Grimes.Ft char *
518de6c267SEd Schouten.Fn initstate "unsigned int seed" "char *state" "size_t n"
5258f0484fSRodney W. Grimes.Ft char *
5358f0484fSRodney W. Grimes.Fn setstate "char *state"
5458f0484fSRodney W. Grimes.Sh DESCRIPTION
55edab57e0SDag-Erling Smørgrav.Bf -symbolic
56eb51316bSColin PercivalThe functions described in this manual page are not secure.
57eb51316bSColin PercivalApplications which require unpredictable random numbers should use
58edab57e0SDag-Erling Smørgrav.Xr arc4random 3
59edab57e0SDag-Erling Smørgravinstead.
60edab57e0SDag-Erling Smørgrav.Ef
61edab57e0SDag-Erling Smørgrav.Pp
62825a73c3SConrad MeyerUnless initialized with less than 32 bytes of state, the
6358f0484fSRodney W. Grimes.Fn random
6458f0484fSRodney W. Grimesfunction
6558f0484fSRodney W. Grimesuses a non-linear additive feedback random number generator employing a
6658f0484fSRodney W. Grimesdefault table of size 31 long integers to return successive pseudo-random
6758f0484fSRodney W. Grimesnumbers in the range from 0 to
6858f0484fSRodney W. Grimes.if t 2\u\s731\s10\d\(mi1.
6958f0484fSRodney W. Grimes.if n (2**31)\(mi1.
7058f0484fSRodney W. GrimesThe period of this random number generator is very large, approximately
7158f0484fSRodney W. Grimes.if t 16\(mu(2\u\s731\s10\d\(mi1).
7258f0484fSRodney W. Grimes.if n 16*((2**31)\(mi1).
7358f0484fSRodney W. Grimes.Pp
74825a73c3SConrad MeyerIf initialized with less than 32 bytes of state,
75825a73c3SConrad Meyer.Fn random
76*672e1225SConrad Meyeruses the poor-quality 32-bit Park-Miller LCG.
77825a73c3SConrad Meyer.Pp
7858f0484fSRodney W. GrimesThe
79064f0074SMike Pritchard.Fn random
80064f0074SMike Pritchardand
81064f0074SMike Pritchard.Fn srandom
82825a73c3SConrad Meyerfunctions are analagous to
83064f0074SMike Pritchard.Xr rand 3
84064f0074SMike Pritchardand
85825a73c3SConrad Meyer.Xr srand 3 .
8658f0484fSRodney W. Grimes.Pp
8740f8b70dSAndrey A. ChernovLike
8858f0484fSRodney W. Grimes.Xr rand 3 ,
8958f0484fSRodney W. Grimes.Fn random
90825a73c3SConrad Meyeris implicitly initialized as if
91825a73c3SConrad Meyer.Fn srandom "1"
92825a73c3SConrad Meyerhad been invoked explicitly.
9358f0484fSRodney W. Grimes.Pp
9458f0484fSRodney W. GrimesThe
957b0b1b2fSAndrey A. Chernov.Fn srandomdev
96825a73c3SConrad Meyerroutine initializes the state array using random numbers obtained from the
97825a73c3SConrad Meyerkernel.
98825a73c3SConrad MeyerThis can generate states which are impossible to reproduce by calling
99825a73c3SConrad Meyer.Fn srandom ,
100825a73c3SConrad Meyerbecause the succeeding terms in the state buffer are no longer derived from the
101825a73c3SConrad MeyerPark-Miller LCG algorithm applied to a fixed seed.
1027b0b1b2fSAndrey A. Chernov.Pp
1037b0b1b2fSAndrey A. ChernovThe
10458f0484fSRodney W. Grimes.Fn initstate
105825a73c3SConrad Meyerroutine initializes the provided state array of
106825a73c3SConrad Meyer.Vt uint32_t
107825a73c3SConrad Meyervalues and uses it in future
108825a73c3SConrad Meyer.Fn random
109825a73c3SConrad Meyerinvocations.
110825a73c3SConrad Meyer(Despite the
111825a73c3SConrad Meyer.Vt char *
112825a73c3SConrad Meyertype of
113825a73c3SConrad Meyer.Fa state ,
114825a73c3SConrad Meyerthe underlying object must be a naturally aligned array of 32-bit values.)
1151a0a9345SRuslan ErmilovThe size of the state array (in bytes) is used by
11658f0484fSRodney W. Grimes.Fn initstate
11758f0484fSRodney W. Grimesto decide how sophisticated a random number generator it should use \(em the
11858f0484fSRodney W. Grimesmore state, the better the random numbers will be.
11958f0484fSRodney W. Grimes(Current "optimal" values for the amount of state information are
12058f0484fSRodney W. Grimes8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
1211a0a9345SRuslan Ermilovthe nearest known amount.
1221a0a9345SRuslan ErmilovUsing less than 8 bytes will cause an error.)
123825a73c3SConrad MeyerThe
124825a73c3SConrad Meyer.Fa seed
125825a73c3SConrad Meyeris used as in
126825a73c3SConrad Meyer.Fn srandom .
12758f0484fSRodney W. GrimesThe
12858f0484fSRodney W. Grimes.Fn initstate
12958f0484fSRodney W. Grimesfunction
13058f0484fSRodney W. Grimesreturns a pointer to the previous state information array.
13158f0484fSRodney W. Grimes.Pp
13258f0484fSRodney W. GrimesThe
13358f0484fSRodney W. Grimes.Fn setstate
134825a73c3SConrad Meyerroutine switches
135825a73c3SConrad Meyer.Fn random
136825a73c3SConrad Meyerto using the provided state.
137825a73c3SConrad MeyerIt returns a pointer to the previous state.
13858f0484fSRodney W. Grimes.Pp
13958f0484fSRodney W. GrimesOnce a state array has been initialized, it may be restarted at a
14058f0484fSRodney W. Grimesdifferent point either by calling
14158f0484fSRodney W. Grimes.Fn initstate
14258f0484fSRodney W. Grimes(with the desired seed, the state array, and its size) or by calling
14358f0484fSRodney W. Grimesboth
14458f0484fSRodney W. Grimes.Fn setstate
14558f0484fSRodney W. Grimes(with the state array) and
14658f0484fSRodney W. Grimes.Fn srandom
14758f0484fSRodney W. Grimes(with the desired seed).
14858f0484fSRodney W. GrimesThe advantage of calling both
14958f0484fSRodney W. Grimes.Fn setstate
15058f0484fSRodney W. Grimesand
15158f0484fSRodney W. Grimes.Fn srandom
15258f0484fSRodney W. Grimesis that the size of the state array does not have to be remembered after
15358f0484fSRodney W. Grimesit is initialized.
15458f0484fSRodney W. Grimes.Pp
15558f0484fSRodney W. GrimesWith 256 bytes of state information, the period of the random number
15658f0484fSRodney W. Grimesgenerator is greater than
15758f0484fSRodney W. Grimes.if t 2\u\s769\s10\d,
15858f0484fSRodney W. Grimes.if n 2**69
15958f0484fSRodney W. Grimeswhich should be sufficient for most purposes.
16058f0484fSRodney W. Grimes.Sh DIAGNOSTICS
16158f0484fSRodney W. GrimesIf
16258f0484fSRodney W. Grimes.Fn initstate
16358f0484fSRodney W. Grimesis called with less than 8 bytes of state information, or if
16458f0484fSRodney W. Grimes.Fn setstate
16516bf528dSAndrey A. Chernovdetects that the state information has been garbled,
16616bf528dSAndrey A. ChernovNULL is returned.
16758f0484fSRodney W. Grimes.Sh SEE ALSO
168e02cc8e4SDavid Schultz.Xr arc4random 3 ,
169866dcc94SKonstantin Belousov.Xr lrand48 3 ,
17040f8b70dSAndrey A. Chernov.Xr rand 3 ,
171df49b8e2SDima Dorfman.Xr random 4
17258f0484fSRodney W. Grimes.Sh HISTORY
17358f0484fSRodney W. GrimesThese
17458f0484fSRodney W. Grimesfunctions appeared in
17558f0484fSRodney W. Grimes.Bx 4.2 .
17624a0682cSRuslan Ermilov.Sh AUTHORS
17724a0682cSRuslan Ermilov.An Earl T. Cohen
178