summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/random.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/random.3')
-rw-r--r--src/lib/libc/stdlib/random.339
1 files changed, 22 insertions, 17 deletions
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3
index 260c239816..d3a3198ebf 100644
--- a/src/lib/libc/stdlib/random.3
+++ b/src/lib/libc/stdlib/random.3
@@ -29,7 +29,7 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" $OpenBSD: random.3,v 1.11 2000/04/03 23:23:48 millert Exp $ 32.\" $OpenBSD: random.3,v 1.12 2000/04/20 13:50:02 aaron Exp $
33.\" 33.\"
34.Dd April 19, 1991 34.Dd April 19, 1991
35.Dt RANDOM 3 35.Dt RANDOM 3
@@ -56,23 +56,27 @@
56.Sh DESCRIPTION 56.Sh DESCRIPTION
57The 57The
58.Fn random 58.Fn random
59function 59function uses a non-linear additive feedback random number generator employing
60uses a non-linear additive feedback random number generator employing a 60a default table of size 31 long integers to return successive pseudo-random
61default table of size 31 long integers to return successive pseudo-random
62numbers in the range from 0 to (2**31)\-1. 61numbers in the range from 0 to (2**31)\-1.
63The period of this random number generator is very large, approximately 62The period of this random number generator is very large, approximately
6416*((2**31)\-1. 6316*((2**31)\-1.
65.Pp 64.Pp
66The 65The
67.Fn random Ns / Fn srandom 66.Fn random
68have (almost) the same calling sequence and initialization properties as 67and
68.Fn srandom
69functions have (almost) the same calling sequence and initialization
70properties as
69.Xr rand 3 Ns / Xr srand 3 . 71.Xr rand 3 Ns / Xr srand 3 .
70The difference is that 72The difference is that
71.Xr rand 73.Xr rand
72produces a much less random sequence \(em in fact, the low dozen bits 74produces a much less random sequence \(em in fact, the low dozen bits
73generated by rand go through a cyclic pattern. All the bits generated by 75generated by rand go through a cyclic pattern.
76All the bits generated by
74.Fn random 77.Fn random
75are usable. For example, 78are usable.
79For example,
76.Sq Li random()&01 80.Sq Li random()&01
77will produce a random binary 81will produce a random binary
78value. 82value.
@@ -81,9 +85,10 @@ Unlike
81.Xr srand , 85.Xr srand ,
82.Fn srandom 86.Fn srandom
83does not return the old seed; the reason for this is that the amount of 87does not return the old seed; the reason for this is that the amount of
84state information used is much more than a single word. (Two other 88state information used is much more than a single word.
85routines are provided to deal with restarting/changing random 89(Two other routines are provided to deal with restarting/changing random
86number generators). Like 90number generators).
91Like
87.Xr rand 3 , 92.Xr rand 3 ,
88however, 93however,
89.Fn random 94.Fn random
@@ -110,28 +115,28 @@ a fixed seed.
110The 115The
111.Fn initstate 116.Fn initstate
112routine allows a state array, passed in as an argument, to be initialized 117routine allows a state array, passed in as an argument, to be initialized
113for future use. The size of the state array (in bytes) is used by 118for future use.
119The size of the state array (in bytes) is used by
114.Fn initstate 120.Fn initstate
115to decide how sophisticated a random number generator it should use \(em the 121to decide how sophisticated a random number generator it should use \(em the
116more state, the better the random numbers will be. 122more state, the better the random numbers will be.
117(Current "optimal" values for the amount of state information are 123(Current "optimal" values for the amount of state information are
1188, 32, 64, 128, and 256 bytes; other amounts will be rounded down to 1248, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
119the nearest known amount. Using less than 8 bytes will cause an error.) 125the nearest known amount.
126Using less than 8 bytes will cause an error.)
120The seed for the initialization (which specifies a starting point for 127The seed for the initialization (which specifies a starting point for
121the random number sequence, and provides for restarting at the same 128the random number sequence, and provides for restarting at the same
122point) is also an argument. 129point) is also an argument.
123The 130The
124.Fn initstate 131.Fn initstate
125function 132function returns a pointer to the previous state information array.
126returns a pointer to the previous state information array.
127.Pp 133.Pp
128Once a state has been initialized, the 134Once a state has been initialized, the
129.Fn setstate 135.Fn setstate
130routine provides for rapid switching between states. 136routine provides for rapid switching between states.
131The 137The
132.Fn setstate 138.Fn setstate
133function 139function returns a pointer to the previous state array; its
134returns a pointer to the previous state array; its
135argument state array is used for further random number generation 140argument state array is used for further random number generation
136until the next call to 141until the next call to
137.Fn initstate 142.Fn initstate