Previous: , Up: Pseudo-Random Numbers   [Contents][Index]


19.8.4 High Quality Random Number Functions

This section describes the random number functions provided as a GNU extension, based on OpenBSD interfaces.

The GNU C Library uses kernel entropy obtained either through getrandom or by reading /dev/urandom to seed.

These functions provide higher random quality than ISO, BSD, and SVID functions, and may be used in cryptographic contexts.

The prototypes for these functions are in stdlib.h.

Function: uint32_t arc4random (void)

| MT-Safe | AS-Unsafe corrupt | AC-Safe | See POSIX Safety Concepts.

This function returns a single 32-bit value in the range of 0 to 2^32−1 (inclusive), which is twice the range of rand and random.

Function: void arc4random_buf (void *buffer, size_t length)

| MT-Safe | AS-Unsafe corrupt | AC-Safe | See POSIX Safety Concepts.

This function fills the region buffer of length length bytes with random data.

Function: uint32_t arc4random_uniform (uint32_t upper_bound)

| MT-Safe | AS-Unsafe corrupt | AC-Safe | See POSIX Safety Concepts.

This function returns a single 32-bit value, uniformly distributed but less than the upper_bound. It avoids the modulo bias when the upper bound is not a power of two.