1ARC4RANDOM(3bsd)                     LOCAL                    ARC4RANDOM(3bsd)
2

NAME

4     arc4random, arc4random_buf, arc4random_uniform, arc4random_stir,
5     arc4random_addrandom — arc4 random number generator
6

LIBRARY

8     Utility functions from BSD systems (libbsd, -lbsd)
9

SYNOPSIS

11     #include <stdlib.h>
12     (See libbsd(7) for include usage.)
13
14     uint32_t
15     arc4random(void);
16
17     void
18     arc4random_buf(void *buf, size_t nbytes);
19
20     uint32_t
21     arc4random_uniform(uint32_t upper_bound);
22
23     void
24     arc4random_stir(void);
25
26     void
27     arc4random_addrandom(unsigned char *dat, int datlen);
28

DESCRIPTION

30     This family of functions provides higher quality data than those
31     described in rand(3), random(3), and rand48(3).
32
33     Use of these functions is encouraged for almost all random number con‐
34     sumption because the other interfaces are deficient in either quality,
35     portability, standardization, or availability.  These functions can be
36     called in almost all coding environments, including pthreads(3) and
37     chroot(2).
38
39     High quality 32-bit pseudo-random numbers are generated very quickly.  On
40     each call, a cryptographic pseudo-random number generator is used to gen‐
41     erate a new result.  One data pool is used for all consumers in a
42     process, so that consumption under program flow can act as additional
43     stirring.  The subsystem is re-seeded from the kernel random number sub‐
44     system using getentropy(2) on a regular basis, and also upon fork(2).
45
46     The arc4random() function returns a single 32-bit value.
47
48     The arc4random_buf() function fills the region buf of length nbytes with
49     random data.
50
51     arc4random_uniform() will return a single 32-bit value, uniformly dis‐
52     tributed but less than upper_bound.  This is recommended over construc‐
53     tions like “arc4random() % upper_bound” as it avoids "modulo bias" when
54     the upper bound is not a power of two.  In the worst case, this function
55     may consume multiple iterations to ensure uniformity; see the source code
56     to understand the problem and solution.
57
58     The arc4random_stir() function reads data from getentropy(2) and uses it
59     to re-seed the subsystem via arc4random_addrandom().
60
61     There is no need to call arc4random_stir() before using arc4random()
62     functions family, since they automatically initialize themselves.
63

RETURN VALUES

65     These functions are always successful, and no return value is reserved to
66     indicate an error.
67

SEE ALSO

69     rand(3), rand48(3), random(3)
70

HISTORY

72     These functions first appeared in OpenBSD 2.1, FreeBSD 3.0, NetBSD 1.6,
73     and DragonFly 1.0.
74
75     The original version of this random number generator used the RC4 (also
76     known as ARC4) algorithm.  In OpenBSD 5.5 it was replaced with the
77     ChaCha20 cipher, and it may be replaced again in the future as crypto‐
78     graphic techniques advance.  A good mnemonic is “A Replacement Call for
79     Random”.
80
81BSD                              June 20, 2019                             BSD
Impressum