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)
162 value =
strtoll(text, &end, 10);
163 if (end == text || *end !=
'\0')
164 return _(
"must be an integer with nothing after it");
168 return _(
"must be between -2147483648 and 4294967295");
216 if (seed_from_environment(
"GRASS_RANDOM_SEED", &
given) ||
217 seed_from_environment(
"SOURCE_DATE_EPOCH", &
given))
222#ifdef HAVE_GETTIMEOFDAY
289static void G__next(
void)
325 return (
long)(G__next() >> 17);
370 return (
double)G__next() / 281474976710656.0;
380 r /= 281474976710656.0;
427 c = (mul48(a, c) + c) &
MASK48;
441 if (!seed_in_range(
seed))
443 "from -2147483648 to 4294967295 the generator can use"),
447static void check_units(
int64_t units)
450 G_fatal_error(
_(
"The number of units of a random number layout must "
451 "be positive, not %" PRId64),
482 layout->whole_span =
false;
542 G_fatal_error(
_(
"The number of random numbers a unit draws must be "
543 "positive, not %" PRId64),
547 "%" PRId64 " draws each is too large (the number of "
548 "draws must not exceed %" PRId64 ")"),
586 "positive, not %" PRId64),
592 "most %" PRId64 " draws each is too large (the number "
593 "of units times the bound rounded up to odd must not "
640 G_fatal_error(
_(
"A random number layout of the whole span takes at "
642 "layout serves any number of units"),
646 if (
parts > 1 && stride % 2 == 0)
652 layout->whole_span =
true;
716 "(must be between 0 and %" PRId64 ")"),
720 "(must not be negative)"),
724 "(a layout of the whole span holds a single batch)"),
728 "(%" PRId64 " batch fits into the generator's span)",
729 "Random number batch %" PRId64 " is out of range "
730 "(%" PRId64 " batches fit into the generator's span)",
731 (
unsigned long)
layout->batches),
735 "(no batch fits into the generator's span, only batch "
778 "%" PRId64 " draws (the number must not be negative)"),
801 return (
double)
state->state / 281474976710656.0;
void void void void G_fatal_error(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.
int32_t G_random_int32(struct G_random_state *state)
Generate an integer in the range [-2^31, 2^31)
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.
int G_random_int(struct G_random_state *state)
Generate a non-negative integer in the range [0, 2^31)
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.
uint32_t G_random_uint32(struct G_random_state *state)
Generate an integer in the range [0, 2^32)
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)
const char * G_random_parse_seed(const char *text, int64_t *seed)
Read a random number seed from a text.
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 *)