RAND(3) Library Functions Manual RAND(3)

RandThread-safe random numbers.

#include <Rand.h>

int
RandInt(unsigned int);

void
RandIntN(int *, size_t, unsigned int);

Rand is used for generating random numbers in a thread-safe way. Currently, one seed is shared across all threads, which means only one thread can generate random numbers at a time. In the future, a seed pool may be maintained. The seed is initialized on the first call to a function that needs it. It is initialized with the current timestamp, the process ID, and the thread ID. These should be sufficiently random sources, so the seed should be secure enough.

() generates a single random integer between 0 and the passed value. () takes an integer pointer, a buffer size, and the maximum value a random number is allowed to be. It generates the number of random integers specified by the buffer size, and stores them at the passed pointer. This allows a caller to get multiple random numbers at a time, as each call to RandInt() will have to lock and unlock a mutex, whereas RandIntN() can obtain multiple random integers in a single pass.

RandInt() returns the value of rand_r(3) with the internally-stored seed. The return value should be in the range of 0 to RAND_MAX.

Util(3), rand(3)

February 16, 2023 Telodendria Project