xref: /386bsd/usr/share/man/cat3/initstate.0 (revision a2142627)
1RANDOM(3)                 386BSD Programmer's Manual                 RANDOM(3)
2
3NNAAMMEE
4     rraannddoomm, ssrraannddoomm, iinniittssttaattee, sseettssttaattee - better random number generator;
5     routines for changing generators
6
7SSYYNNOOPPSSIISS
8     ##iinncclluuddee <<ssttddlliibb>>
9
10     _l_o_n_g
11     rraannddoomm(_v_o_i_d)
12
13     _v_o_i_d
14     ssrraannddoomm(_u_n_s_i_g_n_e_d _s_e_e_d)
15
16     _c_h_a_r *
17     iinniittssttaattee(_u_n_s_i_g_n_e_d _s_e_e_d, _c_h_a_r *_s_t_a_t_e, _i_n_t _n)
18
19     _c_h_a_r *
20     sseettssttaattee(_c_h_a_r *_s_t_a_t_e)
21
22DDEESSCCRRIIPPTTIIOONN
23     The rraannddoomm() function uses a non-linear additive feedback random number
24     generator employing a default table of size 31 long integers to return
25     successive pseudo-random numbers in the range from 0 to (2**31)-1.  The
26     period of this random number generator is very large, approximately
27     16*((2**31)-1).
28
29     The rraannddoomm()/ ssrraannddoomm() have (almost) the same calling sequence and
30     initialization properties as rand(3)/ srand(3).   The difference is that
31     rand produces a much less random sequence - in fact, the low dozen bits
32     generated by rand go through a cyclic pattern.  All the bits generated by
33     rraannddoomm() are usable.  For example, `random()&01' will produce a random
34     binary value.
35
36     Unlike srand,  ssrraannddoomm() does not return the old seed; the reason for
37     this is that the amount of state information used is much more than a
38     single word.  (Two other routines are provided to deal with
39     restarting/changing random number generators).  Like rand(3),  however,
40     rraannddoomm() will by default produce a sequence of numbers that can be
41     duplicated by calling ssrraannddoomm() with `1' as the seed.
42
43     The iinniittssttaattee() routine allows a state array, passed in as an argument,
44     to be initialized for future use.  The size of the state array (in bytes)
45     is used by iinniittssttaattee() to decide how sophisticated a random number
46     generator it should use - the more state, the better the random numbers
47     will be.  (Current "optimal" values for the amount of state information
48     are 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
49     the nearest known amount.  Using less than 8 bytes will cause an error.)
50     The seed for the initialization (which specifies a starting point for the
51     random number sequence, and provides for restarting at the same point) is
52     also an argument.  The iinniittssttaattee() function returns a pointer to the
53     previous state information array.
54
55     Once a state has been initialized, the sseettssttaattee() routine provides for
56     rapid switching between states.  The sseettssttaattee() function returns a
57     pointer to the previous state array; its argument state array is used for
58     further random number generation until the next call to iinniittssttaattee() or
59     sseettssttaattee().
60
61     Once a state array has been initialized, it may be restarted at a
62     different point either by calling iinniittssttaattee() (with the desired seed, the
63     state array, and its size) or by calling both sseettssttaattee() (with the state
64     array) and ssrraannddoomm() (with the desired seed).  The advantage of calling
65     both sseettssttaattee() and ssrraannddoomm() is that the size of the state array does
66     not have to be remembered after it is initialized.
67
68     With 256 bytes of state information, the period of the random number
69     generator is greater than 2**69 which should be sufficient for most
70     purposes.
71
72AAUUTTHHOORR
73     Earl T. Cohen
74
75DDIIAAGGNNOOSSTTIICCSS
76     If iinniittssttaattee() is called with less than 8 bytes of state information, or
77     if sseettssttaattee() detects that the state information has been garbled, error
78     messages are printed on the standard error output.
79
80SSEEEE AALLSSOO
81     rand(3)
82
83HHIISSTTOORRYY
84     These functions appeared in 4.2BSD.
85
86BBUUGGSS
87     About 2/3 the speed of rand(3).
88
894.2 Berkeley Distribution       April 19, 1991                               2
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133