45#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
46 !defined(__STDC_NO_ATOMICS__)
48#define LRAND48_ATOMIC 1
50#define LRAND48_ATOMIC 0
56#ifdef HAVE_GETTIMEOFDAY
69#define LCG_A UINT64_C(0x5DEECE66D)
70#define LCG_B UINT64_C(0xB)
71#define MASK48 UINT64_C(0xFFFFFFFFFFFF)
77#define LCG_SPAN (UINT64_C(1) << 46)
81#define WHOLE_SPAN_MAX_UNITS (INT64_C(1) << 20)
95static const uint32 a0 = 0xE66D;
96static const uint32 a1 = 0xDEEC;
97static const uint32 a2 = 0x5;
99static const uint32 b0 = 0xB;
105#define LO(x) ((x) & 0xFFFFU)
106#define HI(x) ((x) >> 16)
150 value =
strtoll(text, &end, 10);
153 _(
"Random number seed %s from %s is too large in magnitude "
156 if (end == text || *end !=
'\0')
157 G_fatal_error(
_(
"Random number seed %s from %s is not an integer"),
159 if (!seed_in_range(value)) {
201 if (seed_from_environment(
"GRASS_RANDOM_SEED", &
given) ||
202 seed_from_environment(
"SOURCE_DATE_EPOCH", &
given))
207#ifdef HAVE_GETTIMEOFDAY
274static void G__next(
void)
310 return (
long)(G__next() >> 17);
355 return (
double)G__next() / 281474976710656.0;
365 r /= 281474976710656.0;
415 if (!seed_in_range(
seed))
417 "from -2147483648 to 4294967295 the generator can use"),
421static void check_units(
int64_t units)
424 G_fatal_error(
_(
"The number of units of a random number layout must "
425 "be positive, not %" PRId64),
456 layout->whole_span =
false;
516 G_fatal_error(
_(
"The number of random numbers a unit draws must be "
517 "positive, not %" PRId64),
521 "%" PRId64 " draws each is too large (the number of "
522 "draws must not exceed %" PRId64 ")"),
560 "positive, not %" PRId64),
566 "most %" PRId64 " draws each is too large (the number "
567 "of units times the bound rounded up to odd must not "
614 G_fatal_error(
_(
"A random number layout of the whole span takes at "
616 "layout serves any number of units"),
620 if (
parts > 1 && stride % 2 == 0)
626 layout->whole_span =
true;
690 "(must be between 0 and %" PRId64 ")"),
694 "(must not be negative)"),
698 "(a layout of the whole span holds a single batch)"),
702 "(%" PRId64 " batch fits into the generator's span)",
703 "Random number batch %" PRId64 " is out of range "
704 "(%" PRId64 " batches fit into the generator's span)",
705 (
unsigned long)
layout->batches),
709 "(no batch fits into the generator's span, only batch "
752 "%" PRId64 " draws (the number must not be negative)"),
775 return (
double)
state->state / 281474976710656.0;
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
#define n_(strs, strp, num)
void G_random_advance(struct G_random_state *state, int64_t draws)
Advance a generator of the program's own as if values had been drawn.
void G_random_init_layout_bounded(struct G_random_layout *layout, int64_t seed, int64_t units, int64_t max_draws)
Initialize a layout whose units draw at most a number of values.
long G_mrand48(void)
Generate an integer in the range [-2^31, 2^31)
long G_lrand48(void)
Generate an integer in the range [0, 2^31)
#define WHOLE_SPAN_MAX_UNITS
long G_srand48_auto(void)
Seed the pseudo-random number generator from the time and PID.
void G_random_init_layout_exact(struct G_random_layout *layout, int64_t seed, int64_t units, int64_t draws_per_unit)
Initialize a layout whose units draw an exact number of values.
int64_t G_random_layout_length(const struct G_random_layout *layout)
Return the number of values a unit may draw.
void G_random_state_from_seed(struct G_random_state *state, int64_t seed)
Seed a pseudo-random number generator of the program's own.
void G_srand48(long seedval)
Seed the pseudo-random number generator.
int64_t G_random_layout_batches(const struct G_random_layout *layout)
Return the number of batches that fit into the span.
double G_drand48(void)
Generate a floating-point value in the range [0,1)
void G_random_init_layout(struct G_random_layout *layout, int64_t seed, int64_t units)
Initialize a layout which gives the units the whole span.
double G_random_double(struct G_random_state *state)
Generate a floating-point value in the range [0,1) from a generator of the program's own.
void G_random_state_for_unit(struct G_random_state *state, const struct G_random_layout *layout, int64_t unit)
Put a generator state at the start of a unit's stream.
int64_t G_random_generate_seed(void)
Generate a seed for a random number generator.
void G_random_state_for_batch(struct G_random_state *state, const struct G_random_layout *layout, int64_t batch, int64_t unit)
Put a generator state at the start of a unit's stream in a batch.
int gettimeofday(struct timeval *, struct timezone *)