source: trunk/minix/man/man3/random.3@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 3.8 KB
Line 
1.\" Copyright (c) 1983 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
5.\" @(#)random.3 6.2 (Berkeley) 9/29/85
6.\"
7.TH RANDOM 3 "September 29, 1985"
8.UC 5
9.SH NAME
10random, srandom, initstate, setstate \- better random number generator; routines for changing generators
11.SH SYNOPSIS
12.nf
13.ft B
14#include <stdlib.h>
15
16long random(void)
17void srandom(unsigned \fIseed\fP)
18char *initstate(unsigned \fIseed\fP, char *\fIstate\fP, int \fIn\fP)
19char *setstate(char *\fIstate\fP)
20.ft R
21.fi
22.SH DESCRIPTION
23.PP
24.B Random
25uses a non-linear additive feedback random number generator employing a
26default table of size 31 long integers to return successive pseudo-random
27numbers in the range from 0 to
28.if t 2\u\s731\s10\d\(mi1.
29.if n (2**31)\(mi1.
30The period of this random number generator is very large, approximately
31.if t 16\(mu(2\u\s731\s10\d\(mi1).
32.if n 16*((2**31)\(mi1).
33.PP
34.B Random/srandom
35have (almost) the same calling sequence and initialization properties as
36.B rand/srand.
37The difference is that
38.BR rand (3)
39produces a much less random sequence \(em in fact, the low dozen bits
40generated by rand go through a cyclic pattern. All the bits generated by
41.B random
42are usable. For example, ``random()&01'' will produce a random binary
43value.
44.PP
45Unlike
46.BR srand ,
47.B srandom
48does not return the old seed; the reason for this is that the amount of
49state information used is much more than a single word. (Two other
50routines are provided to deal with restarting/changing random
51number generators). Like
52.BR rand (3),
53however,
54.B random
55will by default produce a sequence of numbers that can be duplicated
56by calling
57.B srandom
58with
59.B 1
60as the seed.
61.PP
62The
63.B initstate
64routine allows a state array, passed in as an argument, to be initialized
65for future use. The size of the state array (in bytes) is used by
66.B initstate
67to decide how sophisticated a random number generator it should use -- the
68more state, the better the random numbers will be.
69(Current "optimal" values for the amount of state information are
708, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
71the nearest known amount. Using less than 8 bytes will cause an error).
72The seed for the initialization (which specifies a starting point for
73the random number sequence, and provides for restarting at the same
74point) is also an argument.
75.B Initstate
76returns a pointer to the previous state information array.
77.PP
78Once a state has been initialized, the
79.B setstate
80routine provides for rapid switching between states.
81.B Setstate
82returns a pointer to the previous state array; its
83argument state array is used for further random number generation
84until the next call to
85.B initstate
86or
87.BR setstate .
88.PP
89Once a state array has been initialized, it may be restarted at a
90different point either by calling
91.B initstate
92(with the desired seed, the state array, and its size) or by calling
93both
94.B setstate
95(with the state array) and
96.B srandom
97(with the desired seed).
98The advantage of calling both
99.B setstate
100and
101.B srandom
102is that the size of the state array does not have to be remembered after
103it is initialized.
104.PP
105With 256 bytes of state information, the period of the random number
106generator is greater than
107.if t 2\u\s769\s10\d,
108.if n 2**69
109which should be sufficient for most purposes.
110.SH AUTHOR
111Earl T. Cohen
112.SH DIAGNOSTICS
113.PP
114If
115.B initstate
116is called with less than 8 bytes of state information, or if
117.B setstate
118detects that the state information has been garbled, error
119messages are printed on the standard error output.
120.SH "SEE ALSO"
121.BR rand (3).
122.SH NOTES
123.B initstate
124and
125.B setstate
126are not declared in
127.IR <stdlib.h> ,
128programmers must provide their own declarations.
129.SH BUGS
130About 2/3 the speed of
131.BR rand (3).
Note: See TracBrowser for help on using the repository browser.