GETENTROPY(3) Library Functions Manual GETENTROPY(3)

getentropy
fill a buffer with high quality random data

Standard C Library (libc, -lc)

#include <unistd.h>

int
getentropy(void *buf, size_t buflen);

The getentropy() function fills a buffer with high quality random data, suitable for seeding cryptographically secure pseudo-random number generators.

getentropy() is only intended for seeding random number generators and is not intended for use by regular code which simply needs secure random data. For this purpose, please use arc4random(3).

The maximum value for buflen is 256 bytes.

getentropy() reads from the sysctl(7) variable kern.arandom.

The getentropy() function returns 0 on success, and -1 if an error occurred.

getentropy() will succeed unless:
[]
The buf argument points to an invalid memory address.
[]
Too many bytes were requested.

arc4random(3), rnd(4)

The getentropy() function is non-standard.

The getentropy() function first appeared in OpenBSD 5.6, then in FreeBSD 12.0, and NetBSD 10.
May 1, 2020 NetBSD 9.99