1 /*
2 Copyright (c) 1990 Regents of the University of California.
3 All rights reserved.
4 */
5 /*
6 FUNCTION
7 <<rand>>, <<srand>>---pseudo-random numbers
8
9 INDEX
10 rand
11 INDEX
12 srand
13 INDEX
14 rand_r
15
16 SYNOPSIS
17 #include <stdlib.h>
18 int rand(void);
19 void srand(unsigned int <[seed]>);
20 int rand_r(unsigned int *<[seed]>);
21
22 DESCRIPTION
23 <<rand>> returns a different integer each time it is called; each
24 integer is chosen by an algorithm designed to be unpredictable, so
25 that you can use <<rand>> when you require a random number.
26 The algorithm depends on a static variable called the ``random seed'';
27 starting with a given value of the random seed always produces the
28 same sequence of numbers in successive calls to <<rand>>.
29
30 You can set the random seed using <<srand>>; it does nothing beyond
31 storing its argument in the static variable used by <<rand>>. You can
32 exploit this to make the pseudo-random sequence less predictable, if
33 you wish, by using some other unpredictable value (often the least
34 significant parts of a time-varying value) as the random seed before
35 beginning a sequence of calls to <<rand>>; or, if you wish to ensure
36 (for example, while debugging) that successive runs of your program
37 use the same ``random'' numbers, you can use <<srand>> to set the same
38 random seed at the outset.
39
40 RETURNS
41 <<rand>> returns the next pseudo-random integer in sequence; it is a
42 number between <<0>> and <<RAND_MAX>> (inclusive).
43
44 <<srand>> does not return a result.
45
46 NOTES
47 <<rand>> and <<srand>> are unsafe for multi-threaded applications.
48 <<rand_r>> is thread-safe and should be used instead.
49
50
51 PORTABILITY
52 <<rand>> is required by ANSI, but the algorithm for pseudo-random
53 number generation is not specified; therefore, even if you use
54 the same random seed, you cannot expect the same sequence of results
55 on two different systems.
56
57 <<rand>> requires no supporting OS subroutines.
58 */
59
60 #ifndef _REENT_ONLY
61
62 #define _DEFAULT_SOURCE
63 #include <stdlib.h>
64
65 int
rand(void)66 rand (void)
67 {
68 return (int) random();
69 }
70
71 #endif /* _REENT_ONLY */
72