arc4random(), arc4random_buf(), arc4random_uniform(), arc4random_stir(), arc4random_addrandom()

arc4 random number generator 

Function


SYNOPSIS

#include <stdlib.h>

u_int32_t arc4random(void);

void arc4random_buf(void *buf, size_t nbytes);

u_int32_t arc4random_uniform(u_int32_t upper_bound);

void arc4random_stir(void);

void arc4random_addrandom(unsigned char *dat, int datlen);


DESCRIPTION

The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand() and random().

arc4random_buf() function fills the region buf of length nbytes with ARC4-derived random data.

arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.

The arc4random_stir() function reads data from /dev/urandom and uses it to permute the S-Boxes via arc4random_addrandom().

There is no need to call arc4random_stir() before using arc4random() functions family, since they automatically initialize themselves.


EXAMPLES

The following produces a drop-in replacement for the traditional rand() and random() functions using arc4random():

#define foo4random() (arc4random() % ((unsigned)RAND_MAX + 1))

MULTITHREAD SAFETY LEVEL

Safe.


PORTING ISSUES

No problems expected.


PORTABILITY

FreeBSD 5 and up. Linux. Windows Server 2012. Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
rand(), random()


PTC MKS Toolkit 10.3 Documentation Build 39.