xref: /freebsd/lib/libc/stdlib/random.3 (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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.\" $FreeBSD$
30.\"
31.Dd July 26, 2016
32.Dt RANDOM 3
33.Os
34.Sh NAME
35.Nm random ,
36.Nm srandom ,
37.Nm srandomdev ,
38.Nm initstate ,
39.Nm setstate
40.Nd better random number generator; routines for changing generators
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In stdlib.h
45.Ft long
46.Fn random void
47.Ft void
48.Fn srandom "unsigned int seed"
49.Ft void
50.Fn srandomdev void
51.Ft char *
52.Fn initstate "unsigned int seed" "char *state" "size_t n"
53.Ft char *
54.Fn setstate "char *state"
55.Sh DESCRIPTION
56.Bf -symbolic
57The functions described in this manual page are not secure.
58Applications which require unpredictable random numbers should use
59.Xr arc4random 3
60instead.
61.Ef
62.Pp
63The
64.Fn random
65function
66uses a non-linear additive feedback random number generator employing a
67default table of size 31 long integers to return successive pseudo-random
68numbers in the range from 0 to
69.if t 2\u\s731\s10\d\(mi1.
70.if n (2**31)\(mi1.
71The period of this random number generator is very large, approximately
72.if t 16\(mu(2\u\s731\s10\d\(mi1).
73.if n 16*((2**31)\(mi1).
74.Pp
75The
76.Fn random
77and
78.Fn srandom
79functions have (almost) the same calling sequence and initialization properties as the
80.Xr rand 3
81and
82.Xr srand 3
83functions.
84The difference is that
85.Xr rand 3
86produces a much less random sequence \(em in fact, the low dozen bits
87generated by rand go through a cyclic pattern.
88All the bits generated by
89.Fn random
90are usable.
91For example,
92.Sq Li random()&01
93will produce a random binary
94value.
95.Pp
96Like
97.Xr rand 3 ,
98.Fn random
99will by default produce a sequence of numbers that can be duplicated
100by calling
101.Fn srandom
102with
103.Ql 1
104as the seed.
105.Pp
106The
107.Fn srandomdev
108routine initializes a state array using
109pseudo-random numbers obtained from the kernel.
110Note that this particular seeding
111procedure can generate states which are impossible to reproduce by
112calling
113.Fn srandom
114with any value, since the succeeding terms in the
115state buffer are no longer derived from the LC algorithm applied to
116a fixed seed.
117.Pp
118The
119.Fn initstate
120routine allows a state array, passed in as an argument, to be initialized
121for future use.
122The size of the state array (in bytes) is used by
123.Fn initstate
124to decide how sophisticated a random number generator it should use \(em the
125more state, the better the random numbers will be.
126(Current "optimal" values for the amount of state information are
1278, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
128the nearest known amount.
129Using less than 8 bytes will cause an error.)
130The seed for the initialization (which specifies a starting point for
131the random number sequence, and provides for restarting at the same
132point) is also an argument.
133The
134.Fn initstate
135function
136returns a pointer to the previous state information array.
137.Pp
138Once a state has been initialized, the
139.Fn setstate
140routine provides for rapid switching between states.
141The
142.Fn setstate
143function
144returns a pointer to the previous state array; its
145argument state array is used for further random number generation
146until the next call to
147.Fn initstate
148or
149.Fn setstate .
150.Pp
151Once a state array has been initialized, it may be restarted at a
152different point either by calling
153.Fn initstate
154(with the desired seed, the state array, and its size) or by calling
155both
156.Fn setstate
157(with the state array) and
158.Fn srandom
159(with the desired seed).
160The advantage of calling both
161.Fn setstate
162and
163.Fn srandom
164is that the size of the state array does not have to be remembered after
165it is initialized.
166.Pp
167With 256 bytes of state information, the period of the random number
168generator is greater than
169.if t 2\u\s769\s10\d,
170.if n 2**69
171which should be sufficient for most purposes.
172.Sh DIAGNOSTICS
173If
174.Fn initstate
175is called with less than 8 bytes of state information, or if
176.Fn setstate
177detects that the state information has been garbled,
178NULL is returned.
179.Sh SEE ALSO
180.Xr arc4random 3 ,
181.Xr lrand48 3 ,
182.Xr rand 3 ,
183.Xr random 4
184.Sh HISTORY
185These
186functions appeared in
187.Bx 4.2 .
188.Sh AUTHORS
189.An Earl T. Cohen
190.Sh BUGS
191About 2/3 the speed of
192.Xr rand 3 .
193.Pp
194The historical implementation used to have a very weak seeding; the
195random sequence did not vary much with the seed.
196The current implementation employs a better pseudo-random number
197generator for the initial state calculation.
198