version 1 showing authors affecting page license.
.
Rev |
Author |
# |
Line |
1 |
perry |
1 |
RAND |
|
|
2 |
!!!RAND |
|
|
3 |
NAME |
|
|
4 |
SYNOPSIS |
|
|
5 |
DESCRIPTION |
|
|
6 |
RETURN VALUE |
|
|
7 |
NOTES |
|
|
8 |
CONFORMING TO |
|
|
9 |
SEE ALSO |
|
|
10 |
---- |
|
|
11 |
!!NAME |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
rand, srand - random number generator. |
|
|
15 |
!!SYNOPSIS |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
__#include |
|
|
19 |
__ ''seed''__); |
|
|
20 |
__ |
|
|
21 |
!!DESCRIPTION |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
The __rand()__ function returns a pseudo-random integer |
|
|
25 |
between 0 and __RAND_MAX__. |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
The __srand()__ function sets its argument as the seed |
|
|
29 |
for a new sequence of pseudo-random integers to be returned |
|
|
30 |
by __rand()__. These sequences are repeatable by calling |
|
|
31 |
__srand()__ with the same seed value. |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
If no seed value is provided, the __rand()__ function is |
|
|
35 |
automatically seeded with a value of 1. |
|
|
36 |
!!RETURN VALUE |
|
|
37 |
|
|
|
38 |
|
|
|
39 |
The __rand()__ function returns a value between 0 and |
|
|
40 |
RAND_MAX. The __srand()__ returns no value. |
|
|
41 |
!!NOTES |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
The versions of __rand()__ and __srand()__ in the |
|
|
45 |
Linux C Library use the same random number generator as |
|
|
46 |
__random()__ and __srandom()__, so the lower-order |
|
|
47 |
bits should be as random as the higher-order bits. However, |
|
|
48 |
on older __rand()__ implementations, the lower-order bits |
|
|
49 |
are much less random than the higher-order |
|
|
50 |
bits. |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
In ''Numerical Recipes in C: The Art of Scientific |
|
|
54 |
Computing'' (William H. Press, Brian P. Flannery, Saul A. |
|
|
55 |
Teukolsky, William T. Vetterling; New York: Cambridge |
|
|
56 |
University Press, 1992 (2nd ed., p. 277)), the following |
|
|
57 |
comments are made: |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
j=1+(int) (10.0*rand()/(RAND_MAX+1.0)); |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
and never by anything resembling |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
j=1+(rand() % 10); |
|
|
69 |
|
|
|
70 |
|
|
|
71 |
(which uses lower-order bits). |
|
|
72 |
|
|
|
73 |
|
|
|
74 |
Random-number generation is a complex topic. The |
|
|
75 |
''Numerical Recipes in C'' book (see reference above) |
|
|
76 |
provides an excellent discussion of practical random-number |
|
|
77 |
generation issues in Chapter 7 (Random |
|
|
78 |
Numbers). |
|
|
79 |
|
|
|
80 |
|
|
|
81 |
For a more theoretical discussion which also covers many |
|
|
82 |
practical issues in depth, please see Chapter 3 (Random |
|
|
83 |
Numbers) in Donald E. Knuth's ''The Art of Computer |
|
|
84 |
Programming'', volume 2 (Seminumerical Algorithms), 2nd |
|
|
85 |
ed.; Reading, Massachusetts: Addison-Wesley Publishing |
|
|
86 |
Company, 1981. |
|
|
87 |
!!CONFORMING TO |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
SVID 3, BSD 4.3, ISO 9899 |
|
|
91 |
!!SEE ALSO |
|
|
92 |
|
|
|
93 |
|
|
|
94 |
random(3), srandom(3), initstate(3), |
|
|
95 |
setstate(3) |
|
|
96 |
---- |