Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1906 | serge | 1 | /* |
2 | FUNCTION |
||
3 | < |
||
4 | |||
5 | INDEX |
||
6 | rand |
||
7 | INDEX |
||
8 | srand |
||
9 | INDEX |
||
10 | rand_r |
||
11 | |||
12 | ANSI_SYNOPSIS |
||
13 | #include |
||
14 | int rand(void); |
||
15 | void srand(unsigned int <[seed]>); |
||
16 | int rand_r(unsigned int *<[seed]>); |
||
17 | |||
18 | TRAD_SYNOPSIS |
||
19 | #include |
||
20 | int rand(); |
||
21 | |||
22 | void srand(<[seed]>) |
||
23 | unsigned int <[seed]>; |
||
24 | |||
25 | void rand_r(<[seed]>) |
||
26 | unsigned int *<[seed]>; |
||
27 | |||
28 | |||
29 | DESCRIPTION |
||
30 | < |
||
31 | integer is chosen by an algorithm designed to be unpredictable, so |
||
32 | that you can use < |
||
33 | The algorithm depends on a static variable called the ``random seed''; |
||
34 | starting with a given value of the random seed always produces the |
||
35 | same sequence of numbers in successive calls to < |
||
36 | |||
37 | You can set the random seed using < |
||
38 | storing its argument in the static variable used by < |
||
39 | exploit this to make the pseudo-random sequence less predictable, if |
||
40 | you wish, by using some other unpredictable value (often the least |
||
41 | significant parts of a time-varying value) as the random seed before |
||
42 | beginning a sequence of calls to < |
||
43 | (for example, while debugging) that successive runs of your program |
||
44 | use the same ``random'' numbers, you can use < |
||
45 | random seed at the outset. |
||
46 | |||
47 | RETURNS |
||
48 | < |
||
49 | number between <<0>> and < |
||
50 | |||
51 | < |
||
52 | |||
53 | NOTES |
||
54 | < |
||
55 | < |
||
56 | |||
57 | |||
58 | PORTABILITY |
||
59 | < |
||
60 | number generation is not specified; therefore, even if you use |
||
61 | the same random seed, you cannot expect the same sequence of results |
||
62 | on two different systems. |
||
63 | |||
64 | < |
||
65 | */ |
||
66 | |||
67 | #ifndef _REENT_ONLY |
||
68 | |||
69 | #include |
||
70 | #include |
||
71 | |||
72 | void |
||
73 | _DEFUN (srand, (seed), unsigned int seed) |
||
74 | { |
||
75 | _REENT_CHECK_RAND48(_REENT); |
||
76 | _REENT_RAND_NEXT(_REENT) = seed; |
||
77 | } |
||
78 | |||
79 | int |
||
80 | _DEFUN_VOID (rand) |
||
81 | { |
||
82 | /* This multiplier was obtained from Knuth, D.E., "The Art of |
||
83 | Computer Programming," Vol 2, Seminumerical Algorithms, Third |
||
84 | Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */ |
||
85 | _REENT_CHECK_RAND48(_REENT); |
||
86 | _REENT_RAND_NEXT(_REENT) = |
||
87 | _REENT_RAND_NEXT(_REENT) * __extension__ 6364136223846793005LL + 1; |
||
88 | return (int)((_REENT_RAND_NEXT(_REENT) >> 32) & RAND_MAX); |
||
89 | } |
||
90 | |||
91 | #endif /* _REENT_ONLY */ |