1ARC4RANDOM(3)            BSD Library Functions Manual            ARC4RANDOM(3)
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
13     u_int32_t
14     arc4random(void);
15
16     void
17     arc4random_buf(void *buf, size_t nbytes);
18
19     u_int32_t
20     arc4random_uniform(u_int32_t upper_bound);
21
22     void
23     arc4random_stir(void);
24
25     void
26     arc4random_addrandom(unsigned char *dat, int datlen);
27

DESCRIPTION

29     The arc4random() function uses the key stream generator employed by the
30     arc4 cipher, which uses 8*8 8 bit S-Boxes.  The S-Boxes can be in about
31     (2**1700) states.  The arc4random() function returns pseudo-random num‐
32     bers in the range of 0 to (2**32)−1, and therefore has twice the range of
33     rand(3) and random(3).
34
35     arc4random_buf() function fills the region buf of length nbytes with
36     ARC4-derived random data.
37
38     arc4random_uniform() will return a uniformly distributed random number
39     less than upper_bound.  arc4random_uniform() is recommended over con‐
40     structions like “arc4random() % upper_bound” as it avoids "modulo bias"
41     when the upper bound is not a power of two.
42
43     The arc4random_stir() function reads data from /dev/urandom and uses it
44     to permute the S-Boxes via arc4random_addrandom().
45
46     There is no need to call arc4random_stir() before using arc4random()
47     functions family, since they automatically initialize themselves.
48

EXAMPLES

50     The following produces a drop-in replacement for the traditional rand()
51     and random() functions using arc4random():
52
53           #define foo4random() (arc4random() % ((unsigned)RAND_MAX + 1))
54

SEE ALSO

56     rand(3), random(3), srandomdev(3)
57

HISTORY

59     RC4 has been designed by RSA Data Security, Inc.  It was posted anony‐
60     mously to the USENET and was confirmed to be equivalent by several
61     sources who had access to the original cipher.  Since RC4 used to be a
62     trade secret, the cipher is now referred to as ARC4.
63
64BSD                             April 15, 1997                             BSD
Impressum