1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
.TH RAND 3
.SH NAME
rand, lrand, frand, nrand, lnrand, srand, truerand, ntruerand, fastrand, nfastrand \- random number generator
.SH SYNOPSIS
.B #include <u.h>
.br
.B #include <libc.h>
.PP
.ta \w'\fLdouble 'u
.B
int rand(void)
.PP
.B
long lrand(void)
.PP
.B
double frand(void)
.PP
.B
int nrand(int val)
.PP
.B
long lnrand(long val)
.PP
.B
void srand(long seed)
.PP
.B
ulong truerand(void)
.PP
.B
ulong ntruerand(ulong val)
.sp
.B #include <mp.h>
.br
.B #include <libsec.h>
.PP
.B
void genrandom(uchar *buf, int nbytes)
.PP
.B
void prng(uchar *buf, int nbytes)
.PP
.B
ulong fastrand(void)
.PP
.B
ulong nfastrand(ulong val)
.SH DESCRIPTION
.I Rand
returns a uniform pseudo-random
number
.IR x ,
.RI 0≤ x <2\u\s715\s10\d.
.PP
.I Lrand
returns a uniform
.B long
.IR x ,
.RI 0≤ x <2\u\s731\s10\d.
.PP
.I Frand
returns a uniform
.B double
.IR x ,
.RI 0.0≤ x <1.0,
This function calls
.I lrand
twice to generate a number with as many as 62 significant bits of mantissa.
.PP
.I Nrand
returns a uniform integer
.IR x ,
.RI 0≤ x < val.
.I Lnrand
is the same, but returns a
.BR long .
.PP
The algorithm is additive feedback with:
.IP
x[n] = (x[n\(mi273] + x[n\(mi607]) mod
.if t 2\u\s731\s0\d
.if n 2^31
.LP
giving a period of
.if t 2\u\s730\s10\d \(mu (2\u\s7607\s10\d \- 1).
.if n 2^30 × (2^607 - 1).
.PP
The generators are initialized by calling
.I srand
with whatever you like as argument.
To get a different starting value each time,
.IP
.L
srand(time(0))
.LP
will work as long as it is not called more often
than once per second.
Calling
.IP
.L
srand(1)
.LP
will initialize the generators to their
starting state.
.PP
.I Truerand
returns a random unsigned long read from
.BR /dev/random .
Due to the nature of
.BR /dev/random ,
truerand can only return a few hundred bits a
second.
.PP
.I Ntruerand
returns a uniform random integer
.IR x ,
.RI 0≤ x < val ≤ 2\u\s732\s10\d-1.
.PP
.I Genrandom
fills a buffer with bytes from the X9.17 pseudo-random
number generator. The X9.17 generator is seeded by 24
truly random bytes read from
.BR /dev/random .
.PP
.I Prng
uses the native
.MR rand (3)
pseudo-random number generator to fill the buffer. Used with
.IR srand ,
this function can produce a reproducible stream of pseudo random
numbers useful in testing.
.PP
Both
.I genrandom
and
.I prng
may be passed to
.I mprand
(see
.MR mp (3) ).
.PP
.I Fastrand
uses
.I genrandom
to return a uniform
.B "unsigned long
.IR x ,
.RI 0≤ x < 2\u\s732\s10\d-1.
.PP
.I Nfastrand
uses
.I genrandom
to return a uniform
.B "unsigned long
.IR x ,
.RI 0≤ x < val ≤ 2\u\s732\s10\d-1.
.SH SOURCE
.B \*9/src/lib9
.br
.B \*9/src/libsec/port
.SH "SEE ALSO
.\" .IR cons (3),
.MR mp (3)
.SH BUGS
.I Truerand
and
.I ntruerand
maintain a static file descriptor.
.PP
To avoid name conflicts with the underlying system,
.IR rand ,
.IR lrand ,
.IR frand ,
.IR nrand ,
.IR lnrand ,
and
.I srand
are preprocessor macros defined as
.IR p9rand ,
.IR p9lrand ,
and so on;
see
.MR intro (3) .
.ie \n(HT .ds HT "
.el .ds HT " (see HTML-formatted man page for link)
.PP
Some versions of SunOS do not ship with
.B /dev/random
installed.
You may have to download a
.HR "http://sunsolve.sun.com/search/document.do?assetkey=1-25-27606-1" "patch from Sun\*(HT"
to add it to your system.
|