NAME
Rand
—
Thread-safe random numbers.
SYNOPSIS
#include
<Rand.h>
int
RandInt
(unsigned
int);
void
RandIntN
(int
*, size_t,
unsigned int);
DESCRIPTION
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.
RandInt
()
generates a single random integer between 0 and the passed value.
RandIntN
()
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.
RETURN VALUES
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.