forgot stdlib include line
[unix-history] / usr / src / lib / libc / stdlib / random.3
CommitLineData
02c81f9a
KB
1.\" Copyright (c) 1983 The Regents of the University of California.
2.\" All rights reserved.
7db0d200 3.\"
02c81f9a
KB
4.\" Redistribution and use in source and binary forms are permitted
5.\" provided that the above copyright notice and this paragraph are
6.\" duplicated in all such forms and that any documentation,
7.\" advertising materials, and other materials related to such
8.\" distribution and use acknowledge that the software was developed
9.\" by the University of California, Berkeley. The name of the
10.\" University may not be used to endorse or promote products derived
11.\" from this software without specific prior written permission.
12.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13.\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
7db0d200 15.\"
02c81f9a
KB
16.\" @(#)random.3 6.3 (Berkeley) %G%
17.\"
18.UC 7
0b373d97 19.TH RANDOM 3 ""
7db0d200
KM
20.UC 5
21.SH NAME
22random, srandom, initstate, setstate \- better random number generator; routines for changing generators
23.SH SYNOPSIS
24.nf
25.B long random()
26.PP
27.B srandom(seed)
28.B int seed;
29.PP
30.B char *initstate(seed, state, n)
31.B unsigned seed;
32.B char *state;
33.B int n;
34.PP
35.B char *setstate(state)
36.B char *state;
37.fi
38.SH DESCRIPTION
39.PP
40.I Random
41uses a non-linear additive feedback random number generator employing a
42default table of size 31 long integers to return successive pseudo-random
2bae4d9a
KM
43numbers in the range from 0 to
44.if t 2\u\s731\s10\d\(mi1.
45.if n (2**31)\(mi1.
46The period of this random number generator is very large, approximately
47.if t 16\(mu(2\u\s731\s10\d\(mi1).
48.if n 16*((2**31)\(mi1).
7db0d200
KM
49.PP
50.I Random/srandom
51have (almost) the same calling sequence and initialization properties as
52.I rand/srand.
53The difference is that
54.IR rand (3)
2bae4d9a 55produces a much less random sequence \(em in fact, the low dozen bits
7db0d200
KM
56generated by rand go through a cyclic pattern. All the bits generated by
57.I random
2bae4d9a 58are usable. For example, ``random()&01'' will produce a random binary
7db0d200
KM
59value.
60.PP
61Unlike
62.IR srand ,
63.I srandom
64does not return the old seed; the reason for this is that the amount of
65state information used is much more than a single word. (Two other
66routines are provided to deal with restarting/changing random
67number generators). Like
68.IR rand (3),
69however,
70.I random
71will by default produce a sequence of numbers that can be duplicated
72by calling
73.I srandom
74with
75.I 1
76as the seed.
77.PP
78The
79.I initstate
80routine allows a state array, passed in as an argument, to be initialized
81for future use. The size of the state array (in bytes) is used by
82.I initstate
83to decide how sophisticated a random number generator it should use -- the
84more state, the better the random numbers will be.
85(Current "optimal" values for the amount of state information are
868, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
87the nearest known amount. Using less than 8 bytes will cause an error).
88The seed for the initialization (which specifies a starting point for
89the random number sequence, and provides for restarting at the same
90point) is also an argument.
91.I Initstate
92returns a pointer to the previous state information array.
93.PP
94Once a state has been initialized, the
95.I setstate
96routine provides for rapid switching between states.
0b373d97
KM
97.I Setstate
98returns a pointer to the previous state array; its
7db0d200
KM
99argument state array is used for further random number generation
100until the next call to
101.I initstate
102or
103.I setstate.
104.PP
105Once a state array has been initialized, it may be restarted at a
106different point either by calling
107.I initstate
108(with the desired seed, the state array, and its size) or by calling
109both
110.I setstate
111(with the state array) and
112.I srandom
113(with the desired seed).
114The advantage of calling both
115.I setstate
116and
117.I srandom
118is that the size of the state array does not have to be remembered after
119it is initialized.
120.PP
121With 256 bytes of state information, the period of the random number
2bae4d9a
KM
122generator is greater than
123.if t 2\u\s769\s10\d,
124.if n 2**69
125which should be sufficient for most purposes.
7db0d200
KM
126.SH AUTHOR
127Earl T. Cohen
128.SH DIAGNOSTICS
129.PP
130If
131.I initstate
132is called with less than 8 bytes of state information, or if
133.I setstate
134detects that the state information has been garbled, error
135messages are printed on the standard error output.
136.SH "SEE ALSO"
137rand(3)
138.SH BUGS
139About 2/3 the speed of
140.IR rand (3C).