forked from radfordneal/LDPC-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.html
306 lines (220 loc) · 9.39 KB
/
rand.html
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<HTML><HEAD>
<TITLE> Random Variate Generation Routines </TITLE>
</HEAD><BODY>
<H1> Random Variate Generation Routines </H1>
<P>This module provides facilities for basic pseudo-random number generation,
and for generation of random variates from various common distributions.
<P>All the random generation procedures use the same underlying
"stream" of random numbers. This stream is generated using the
<TT>nrand48</TT> pseudo-random generation procedure found on most Unix
systems, with the output being combined with a limited supply of real
random numbers from the file "randfile" in the source
directory for this software, in order to reduce the chances that the
pseudo-random numbers aren't random enough.
<P>A pseudo-random number stream is determined by an integer
<I>seed</I>, which is typically set by the user for each experimental
run (eg, to 1, 2, 3, etc. for successive runs). The state of the
random number stream can be saved in a structure of type
<TT>rand_state</TT>, and later restored. For example, the state could
be saved in a file at the end of a run, and restored if it is later
decided that the run should be continued for longer.
<P>The methods for generating random variates from various distributions
are mostly taken from the following reference:
<BLOCKQUOTE>
Devroye, L. (1986) <I>Non-Uniform Random Variate Generation</I>,
New York: Springer-Verlag.
</BLOCKQUOTE>
The methods used here are not necessarily the fastest available. They were
selected to be reasonably fast while also being easy to write.
<P><B>Header file required</B>: <TT>rand.h</TT>
<A NAME="get-set-sec">
<P><HR>
<CENTER><BIG>Setting and saving the state</BIG></CENTER>
</A>
<A NAME="seed"><HR><B>rand_seed</B>:
Set the state from an integer seed.</A>
<BLOCKQUOTE><PRE>
void rand_seed
( int seed; /* Seed to set state based on */
)
</PRE></BLOCKQUOTE>
<P>Sets the random number generator to a state that is determined from
the integer <TT>seed</TT>. Setting the seed the same on two occasions,
and then calling exactly the same sequence of random generation
procedures, should produce exactly the same sequence of random
variates. (Note: this may not be true, however, if computers with
different architectures are used on the two occasions.)
<P>Sequential seeds should produce streams that are pretty much
independent (unlike the situation for some pseudo-random number
generators). Setting the seed randomly according to the time or day,
or some such, without recording what it was, is <B>not</B>
recommended, since the results are then not reproducible.
<P><A NAME="get_state"><HR><B>rand_get_state</B>:
Get a pointer to the current state.</A>
<BLOCKQUOTE><PRE>
rand_state *rand_get_state (void)
</PRE></BLOCKQUOTE>
Returns a pointer to the current state of the random number generator,
which is a structure of type <TT>rand_state</TT>. The only use for this
pointer is to use it to save a copy of the current state someplace.
<P><A NAME="use_state"><HR><B>rand_use_state</B>:
Set the state to use from now on.</A>
<BLOCKQUOTE><PRE>
void rand_use_state
( rand_state *state /* Pointer to a previously saved state */
)
</PRE></BLOCKQUOTE>
Sets the random number generator to the state pointed to by the argument.
This state must be valid. The only way to get a valid state is by using
<TT>rand_get_state</TT>.
<A NAME="uniform-sec">
<P><HR>
<CENTER><BIG>Generating uniform random variates</BIG></CENTER>
</A>
<A NAME="uniform"><HR><B>rand_uniform</B>:
Generate uniformly from [0,1).</A>
<BLOCKQUOTE><PRE>
double rand_uniform (void)
</PRE></BLOCKQUOTE>
Returns a random number that is uniformly distributed between 0 and 1,
with a value of exactly 0 being possible, but with a value of exactly 1
not being possible.
<P><A NAME="uniopen"><HR><B>rand_uniopen</B>:
Generate uniformly from (0,1).</A>
<BLOCKQUOTE><PRE>
double rand_uniopen (void)
</PRE></BLOCKQUOTE>
Returns a random number that is uniformly distributed between 0 and 1,
with neither a value of exactly 0 nor a value of exactly 1
being possible.
<A NAME="discrete-sec">
<P><HR>
<CENTER><BIG>Generating discrete random variates</BIG></CENTER>
</A>
<A NAME="int"><HR><B>rand_int</B>:
Pick an integer uniformly from <I>0</I> to <I>n-1</I>.</A>
<BLOCKQUOTE><PRE>
int rand_int
( int n /* Number of integers (from 0) to pick from */
)
</PRE></BLOCKQUOTE>
<P>Randomly picks an integer from the set { <TT>0, 1, ..., n-1</TT> },
with each integer being equally probable. The probabilities may
become somewhat unequal, however, if <TT>n</TT> is very large,
approaching 2<SUP><SMALL>31</SMALL></SUP>.
<P><A NAME="pickd"><HR><B>rand_pickd</B>: Pick an integer from <I>0</I>
to <I>n-1</I> with given probabilities (as doubles).</A>
<BLOCKQUOTE><PRE>
int rand_pickd
( double *p, /* Array of probabilities, of length n */
int n /* Number of integers (from 0) to pick from */
)
</PRE></BLOCKQUOTE>
<P>Randomly picks an integer from the set { <TT>0, 1, ..., n-1</TT> },
with probabilities given by the array of double-precision numbers
passed as the first argument. These numbers need not sum to one, but they
must be non-negative, and must not all be zero. The actual
probabilities used are obtained by dividing each number in this array
by the sum of all the numbers.
<P>If one of the probabilities is exactly zero, it is guaranteed that
the corresponding integer will not be picked.
<P><A NAME="pickf"><HR><B>rand_pickf</B>: Pick an integer from <I>0</I>
to <I>n-1</I> with given probabilities (as floats).</A>
<BLOCKQUOTE><PRE>
int rand_pickf
( float *p, /* Array of probabilities, of length n */
int n /* Number of integers (from 0) to pick from */
)
</PRE></BLOCKQUOTE>
<P>This procedure is the same as <A HREF="#pickd"><TT>rand_pickd</TT></A>
except that the array giving the probabilities is an array of single-precision
floating point numbers, rather than double-precision.
<A NAME="poisson"><HR><B>rand_poisson</B>:
Generate a non-negative integer from a Poisson distribution.</A>
<BLOCKQUOTE><PRE>
int rand_poisson
( double lambda /* Mean of the Poisson distribution */
)
</PRE></BLOCKQUOTE>
<P>Generates a non-negative integer from the Poisson distribution with mean
<tt>lambda</tt>.
<P><A NAME="permutation"><HR><B>rand_permutation</B>: Generate a random
permutation of integers from <i>1</i> to <i>n</i>.</A>
<BLOCKQUOTE><PRE>
int rand_permutation
( int *perm, /* Place to store the permutation */
int n /* Number of integers to permute */
)
</PRE></BLOCKQUOTE>
<P>Stores a random permutation of the integers from <i>1</i> to <i>n</i> in the
array perm, which must be at least <i>n</i> long. All permutations are
equally likely.
<A NAME="continuous-sec">
<P><HR>
<CENTER><BIG>Generating continuous random variates</BIG></CENTER>
</A>
<A NAME="gaussian"><HR><B>rand_gaussian</B>:
Generate a standard Gaussian (normal) random variate.</A>
<BLOCKQUOTE><PRE>
double rand_gaussian (void)
</PRE></BLOCKQUOTE>
<P>Generates a random value drawn from the Gaussian (normal) distribution
with mean zero and standard deviation one, whose density function is
proportional to exp(-<I>x<SUP><SMALL>2</SMALL></SUP></I>/2), with <I>x</I>
being any real value.
<A NAME="logistic"><HR><B>rand_logistic</B>:
Generate a logistic random variate.</A>
<BLOCKQUOTE><PRE>
double rand_logistic (void)
</PRE></BLOCKQUOTE>
<P>Generates a random value drawn from the logistic distribution with
location parameter zero and width parameter one, whose density
function is proportional to
exp(-<I>x</I>) / [1 + exp(-<I>x</I>)]<SUP><SMALL>2</SMALL></SUP>,
with <I>x</I> being any real value.
<P><A NAME="cauchy"><HR><B>rand_cauchy</B>:
Generate a Cauchy random variate.</A>
<BLOCKQUOTE><PRE>
double rand_cauchy (void)
</PRE></BLOCKQUOTE>
<P>Generates a random value drawn from the Cauchy distribution with centre
at zero and width one, whose density function is proportional to
1 / (1+<I>x<SUP><SMALL>2</SMALL></SUP></I>), with <I>x</I> being any real value.
<P><A NAME="gamma"><HR><B>rand_gamma</B>:
Generate a gamma-distributed random variate.</A>
<BLOCKQUOTE><PRE>
double rand_gamma
( double A /* Shape parameter */
)
</PRE></BLOCKQUOTE>
<P>Generates a random value drawn from the gamma distribution with
positive shape parameter <I>A</I>, whose density function is proportional
to <I>x<SUP><SMALL>A-1</SMALL></SUP></I> exp(-<I>x</I>), with <I>x</I>
being any positive real. This procedure will never return a value that is
exactly zero.
<P><A NAME="exp"><HR><B>rand_exp</B>:
Generate an exponentially-distributed random variate.</A>
<BLOCKQUOTE><PRE>
double rand_exponential (void)
</PRE></BLOCKQUOTE>
<P>Generates a random value drawn from the exponential distribution with
mean one, whose density function is exp(-<I>x</I>), with <I>x</I> being
any positive real. This procedure will never return a value that is
exactly zero.
<P>Note: This is a common special case of the gamma distribution.
<P><A NAME="beta"><HR><B>rand_beta</B>:
Generate a beta-distributed random variate.</A>
<BLOCKQUOTE><PRE>
double rand_beta
( double A, /* Parameters of distribution */
double B
)
</PRE></BLOCKQUOTE>
<P>Generates a random value drawn from the beta distribution with positive
parameters <I>A</I> and <I>B</I>, whose density function is proportional
to <I>x<SUP><SMALL>A-1</SMALL></SUP>(1-x)<SUP><SMALL>B-1</SMALL></SUP></I>,
with <I>x</I> being any real in the interval (0,1). This procedure
will never return a value that is exactly zero or exactly one.
<HR>
<A HREF="index.html">Back to index for LDPC software</A>
</BODY></HTML>