xref: /netbsd-src/lib/libc/stdlib/random.3 (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1.\"	$NetBSD: random.3,v 1.18 2003/08/07 16:43:43 agc Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     from: @(#)random.3	8.1 (Berkeley) 6/4/93
31.\"
32.Dd June 4, 1993
33.Dt RANDOM 3
34.Os
35.Sh NAME
36.Nm random ,
37.Nm srandom ,
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 long seed"
49.Ft char *
50.Fn initstate "unsigned long seed" "char *state" "size_t n"
51.Ft char *
52.Fn setstate "char *state"
53.Sh DESCRIPTION
54The
55.Fn random
56function
57uses a non-linear additive feedback random number generator employing a
58default table of size 31 long integers to return successive pseudo-random
59numbers in the range from 0 to
60.if t 2\u\s731\s10\d\(mi1.
61.if n (2**31)\(mi1.
62The period of this random number generator is very large, approximately
63.if t 16\(mu(2\u\s731\s10\d\(mi1).
64.if n 16*((2**31)\(mi1).
65.Pp
66The
67.Fn random
68and
69.Fn srandom
70have (almost) the same calling sequence and initialization properties as
71.Xr rand 3
72and
73.Xr srand 3 .
74The difference is that
75.Xr rand 3
76produces a much less random sequence \(em in fact, the low dozen bits
77generated by
78.Xr rand 3
79go through a cyclic pattern.
80All the bits generated by
81.Fn random
82are usable.
83For example,
84.Sq Li random()\*[Am]01
85will produce a random binary value.
86.Pp
87Like
88.Xr rand 3 ,
89.Fn random
90will by default produce a sequence of numbers that can be duplicated
91by calling
92.Fn srandom
93with
94.Ql 1
95as the seed.
96.Pp
97The
98.Fn initstate
99routine allows a state array, passed in as an argument, to be initialized
100for future use.
101The size of the state array (in bytes) is used by
102.Fn initstate
103to decide how sophisticated a random number generator it should use \(em the
104more state, the better the random numbers will be.
105(Current "optimal" values for the amount of state information are
1068, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
107the nearest known amount.
108Using less than 8 bytes will cause an error).
109The seed for the initialization (which specifies a starting point for
110the random number sequence, and provides for restarting at the same
111point) is also an argument.
112The state array passed to
113.Fn initstate
114must be aligned to a 32-bit boundary.
115This can be achieved by using
116a suitably-sized array of ints, and casting the array to char * when
117passing it to
118.Fn initstate .
119The
120.Fn initstate
121function
122returns a pointer to the previous state information array.
123.Pp
124Once a state has been initialized, the
125.Fn setstate
126routine provides for rapid switching between states.
127The
128.Fn setstate
129function
130returns a pointer to the previous state array; its
131argument state array is used for further random number generation
132until the next call to
133.Fn initstate
134or
135.Fn setstate .
136.Pp
137Once a state array has been initialized, it may be restarted at a
138different point either by calling
139.Fn initstate
140(with the desired seed, the state array, and its size) or by calling
141both
142.Fn setstate
143(with the state array) and
144.Fn srandom
145(with the desired seed).
146The advantage of calling both
147.Fn setstate
148and
149.Fn srandom
150is that the size of the state array does not have to be remembered after
151it is initialized.
152.Pp
153With 256 bytes of state information, the period of the random number
154generator is greater than
155.if t 2\u\s769\s10\d,
156.if n 2**69
157which should be sufficient for most purposes.
158.Sh DIAGNOSTICS
159If
160.Fn initstate
161is called with less than 8 bytes of state information, or if
162.Fn setstate
163detects that the state information has been garbled, error
164messages are printed on the standard error output.
165.Sh SEE ALSO
166.Xr rand 3 ,
167.Xr srand 3 ,
168.Xr rnd 4 ,
169.Xr rnd 9
170.Sh HISTORY
171These
172functions appeared in
173.Bx 4.2 .
174.Sh AUTHORS
175.An Earl T. Cohen
176.Sh BUGS
177About 2/3 the speed of
178.Xr rand 3 .
179