19 int *
const p =
gv_calloc((
size_t)bound,
sizeof(
int));
20 for (
int i = 0; i < bound; i++) {
25 for (
int i = bound - 1; i > 0; --i) {
36 assert(bound <= RAND_MAX);
48 const int discard_threshold =
49 RAND_MAX - (int)(((
unsigned)RAND_MAX + 1) % (unsigned)bound);
54 }
while (r > discard_threshold);
66 const uint64_t discard_threshold =
67 UINT64_MAX - (UINT64_MAX - bound + 1) % bound;
73 for (
size_t i = 0; i <
sizeof(uint64_t); ++i) {
76 memcpy((
char *)&r + i, &
byte,
sizeof(
byte));
78 }
while (r > discard_threshold);
86 if (bound > RAND_MAX) {
87 _Static_assert(INT_MAX <= UINT64_MAX,
88 "the `int` type includes non-negative values that do not "
89 "fit in a `uint64_t`, hence some `int` values can never be "
90 "returned by `gv_random_u64`");
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Arithmetic helper functions.
static int random_small(int bound)
handle random number generation, bound ≤ RAND_MAX
uint64_t gv_random_u64(uint64_t bound)
int * gv_permutation(int bound)