21 int *
const p =
gv_calloc((
size_t)bound,
sizeof(
int));
22 for (
int i = 0; i < bound; i++) {
27 for (
int i = bound - 1; i > 0; --i) {
38 assert(bound <= RAND_MAX);
50 const int discard_threshold =
51 RAND_MAX - (int)(((
unsigned)RAND_MAX + 1) % (unsigned)bound);
56 }
while (r > discard_threshold);
68 const uint64_t discard_threshold =
69 UINT64_MAX - (UINT64_MAX - bound + 1) % bound;
75 for (
size_t i = 0; i <
sizeof(uint64_t); ++i) {
78 memcpy((
char *)&r + i, &
byte,
sizeof(
byte));
80 }
while (r > discard_threshold);
88 if (bound > RAND_MAX) {
89 _Static_assert(INT_MAX <= UINT64_MAX,
90 "the `int` type includes non-negative values that do not "
91 "fit in a `uint64_t`, hence some `int` values can never be "
92 "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)