xref: /freebsd/lib/libc/stdlib/random.3 (revision e5b786625f7f82a1fa91e41823332459ea5550f9)
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. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)random.3	8.1 (Berkeley) 6/4/93
29.\"
30.Dd February 1, 2020
31.Dt RANDOM 3
32.Os
33.Sh NAME
34.Nm random ,
35.Nm srandom ,
36.Nm srandomdev ,
37.Nm initstate ,
38.Nm setstate
39.Nd non-cryptographic pseudorandom number generator; routines for changing generators
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In stdlib.h
44.Ft long
45.Fn random void
46.Ft void
47.Fn srandom "unsigned int seed"
48.Ft void
49.Fn srandomdev void
50.Ft char *
51.Fn initstate "unsigned int seed" "char *state" "size_t n"
52.Ft char *
53.Fn setstate "char *state"
54.Sh DESCRIPTION
55.Bf -symbolic
56The functions described in this manual page are not secure.
57Applications which require unpredictable random numbers should use
58.Xr arc4random 3
59instead.
60.Ef
61.Pp
62Unless initialized with less than 32 bytes of state, the
63.Fn random
64function
65uses a non-linear additive feedback random number generator employing a
66default table of size 31 long integers to return successive pseudo-random
67numbers in the range from 0 to
68.if t 2\u\s731\s10\d\(mi1.
69.if n (2**31)\(mi1.
70The period of this random number generator is very large, approximately
71.if t 16\(mu(2\u\s731\s10\d\(mi1).
72.if n 16*((2**31)\(mi1).
73.Pp
74If initialized with less than 32 bytes of state,
75.Fn random
76uses the poor-quality 32-bit Park-Miller LCG.
77.Pp
78The
79.Fn random
80and
81.Fn srandom
82functions are analagous to
83.Xr rand 3
84and
85.Xr srand 3 .
86.Pp
87Like
88.Xr rand 3 ,
89.Fn random
90is implicitly initialized as if
91.Fn srandom "1"
92had been invoked explicitly.
93.Pp
94The
95.Fn srandomdev
96routine initializes the state array using random numbers obtained from the
97kernel.
98This can generate states which are impossible to reproduce by calling
99.Fn srandom ,
100because the succeeding terms in the state buffer are no longer derived from the
101Park-Miller LCG algorithm applied to a fixed seed.
102.Pp
103The
104.Fn initstate
105routine initializes the provided state array of
106.Vt uint32_t
107values and uses it in future
108.Fn random
109invocations.
110(Despite the
111.Vt char *
112type of
113.Fa state ,
114the underlying object must be a naturally aligned array of 32-bit values.)
115The size of the state array (in bytes) is used by
116.Fn initstate
117to decide how sophisticated a random number generator it should use \(em the
118more state, the better the random numbers will be.
119(Current "optimal" values for the amount of state information are
1208, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
121the nearest known amount.
122Using less than 8 bytes will cause an error.)
123The
124.Fa seed
125is used as in
126.Fn srandom .
127The
128.Fn initstate
129function
130returns a pointer to the previous state information array.
131.Pp
132The
133.Fn setstate
134routine switches
135.Fn random
136to using the provided state.
137It returns a pointer to the previous state.
138.Pp
139Once a state array has been initialized, it may be restarted at a
140different point either by calling
141.Fn initstate
142(with the desired seed, the state array, and its size) or by calling
143both
144.Fn setstate
145(with the state array) and
146.Fn srandom
147(with the desired seed).
148The advantage of calling both
149.Fn setstate
150and
151.Fn srandom
152is that the size of the state array does not have to be remembered after
153it is initialized.
154.Pp
155With 256 bytes of state information, the period of the random number
156generator is greater than
157.if t 2\u\s769\s10\d,
158.if n 2**69
159which should be sufficient for most purposes.
160.Sh DIAGNOSTICS
161If
162.Fn initstate
163is called with less than 8 bytes of state information, or if
164.Fn setstate
165detects that the state information has been garbled,
166NULL is returned.
167.Sh SEE ALSO
168.Xr arc4random 3 ,
169.Xr lrand48 3 ,
170.Xr rand 3 ,
171.Xr random 4
172.Sh HISTORY
173These
174functions appeared in
175.Bx 4.2 .
176.Sh AUTHORS
177.An Earl T. Cohen
178