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