1arc4random(3) Library Functions Manual arc4random(3)
2
3
4
6 arc4random, arc4random_uniform, arc4random_buf - cryptographically-se‐
7 cure pseudorandom number generator
8
10 Standard C library (libc, -lc)
11
13 #include <stdlib.h>
14
15 uint32_t arc4random(void);
16 uint32_t arc4random_uniform(uint32_t upper_bound);
17 void arc4random_buf(void buf[.n], size_t n);
18
20 These functions give cryptographically-secure pseudorandom numbers.
21
22 arc4random() returns a uniformly-distributed value.
23
24 arc4random_uniform() returns a uniformly-distributed value less than
25 upper_bound (see BUGS).
26
27 arc4random_buf() fills the memory pointed to by buf, with n bytes of
28 pseudorandom data.
29
30 The rand(3) and drand48(3) families of functions should only be used
31 where the quality of the pseudorandom numbers is not a concern and
32 there's a need for repeatability of the results. Unless you meet both
33 of those conditions, use the arc4random() functions.
34
36 arc4random() returns a pseudorandom number.
37
38 arc4random_uniform() returns a pseudorandom number less than up‐
39 per_bound for valid input, or 0 when upper_bound is invalid.
40
42 For an explanation of the terms used in this section, see at‐
43 tributes(7).
44
45 ┌────────────────────────────────────────────┬───────────────┬─────────┐
46 │Interface │ Attribute │ Value │
47 ├────────────────────────────────────────────┼───────────────┼─────────┤
48 │arc4random(), arc4random_uniform(), │ Thread safety │ MT-Safe │
49 │arc4random_buf() │ │ │
50 └────────────────────────────────────────────┴───────────────┴─────────┘
51
53 BSD.
54
56 OpenBSD 2.1, FreeBSD 3.0, NetBSD 1.6, DragonFly 1.0, libbsd, glibc
57 2.36.
58
60 An upper_bound of 0 doesn't make sense in a call to
61 arc4random_uniform(). Such a call will fail, and return 0. Be
62 careful, since that value is not less than upper_bound. In some cases,
63 such as accessing an array, using that value could result in Undefined
64 Behavior.
65
67 getrandom(3), rand(3), drand48(3), random(7)
68
69
70
71Linux man-pages 6.05 2023-07-20 arc4random(3)