1/*-------------------------------------------------------------------------
3 * Pseudo-Random Number Generator
5 * We use Blackman and Vigna's xoroshiro128** 1.0 algorithm
6 * to have a small, fast PRNG suitable for generating reasonably
7 * good-quality 64-bit data. This should not be considered
8 * cryptographically strong, however.
10 * About these generators: https://prng.di.unimi.it/
11 * See also https://en.wikipedia.org/wiki/List_of_random_number_generators
13 * Copyright (c) 2021-2025, PostgreSQL Global Development Group
15 * src/common/pg_prng.c
17 *-------------------------------------------------------------------------
27/* X/Open (XSI) requires <math.h> to provide M_PI, but core POSIX does not */
29 #define M_PI 3.14159265358979323846
33/* process-wide state vector */
43 return (
x << bits) | (
x >> (64 - bits));
47 * The basic xoroshiro128** algorithm.
48 * Generates and returns a 64-bit uniformly distributed number,
49 * updating the state vector for next time.
51 * Note: the state vector must not be all-zeroes, as that is a fixed point.
61 state->s0 =
rotl(s0, 24) ^ sx ^ (sx << 16);
68 * We use this generator just to fill the xoroshiro128** state vector
77 /* value extraction */
85 * Initialize the PRNG state from a 64-bit integer,
86 * taking care that we don't produce all-zeroes.
93 /* Let's just make sure we didn't get all-zeroes */
98 * Initialize the PRNG state from a double in the range [-1.0, 1.0],
99 * taking care that we don't produce all-zeroes.
104 /* Assume there's about 52 mantissa bits; the sign contributes too. */
111 * Validate a PRNG seed value.
117 * If the seeding mechanism chanced to produce all-zeroes, insert
118 * something nonzero. Anything would do; use Knuth's LCG parameters.
126 /* As a convenience for the pg_prng_strong_seed macro, return true */
131 * Select a random uint64 uniformly from the range [0, PG_UINT64_MAX].
140 * Select a random uint64 uniformly from the range [rmin, rmax].
141 * If the range is empty, rmin is always produced.
151 * Use bitmask rejection method to generate an offset in 0..range.
152 * Each generated val is less than twice "range", so on average we
153 * should not have to iterate more than twice.
170 * Select a random int64 uniformly from the range [PG_INT64_MIN, PG_INT64_MAX].
179 * Select a random int64 uniformly from the range [0, PG_INT64_MAX].
188 * Select a random int64 uniformly from the range [rmin, rmax].
189 * If the range is empty, rmin is always produced.
201 * Use pg_prng_uint64_range(). Can't simply pass it rmin and rmax,
202 * since (uint64) rmin will be larger than (uint64) rmax if rmin < 0.
208 * Safely convert back to int64, avoiding implementation-defined
209 * behavior for values larger than PG_INT64_MAX. Modern compilers
210 * will reduce this to a simple assignment.
224 * Select a random uint32 uniformly from the range [0, PG_UINT32_MAX].
230 * Although xoroshiro128** is not known to have any weaknesses in
231 * randomness of low-order bits, we prefer to use the upper bits of its
232 * result here and below.
236 return (
uint32) (v >> 32);
240 * Select a random int32 uniformly from the range [PG_INT32_MIN, PG_INT32_MAX].
247 return (
int32) (v >> 32);
251 * Select a random int32 uniformly from the range [0, PG_INT32_MAX].
258 return (
int32) (v >> 33);
262 * Select a random double uniformly from the range [0.0, 1.0).
264 * Note: if you want a result in the range (0.0, 1.0], the standard way
265 * to get that is "1.0 - pg_prng_double(state)".
273 * As above, assume there's 52 mantissa bits in a double. This result
274 * could round to 1.0 if double's precision is less than that; but we
275 * assume IEEE float arithmetic elsewhere in Postgres, so this seems OK.
277 return ldexp((
double) (v >> (64 - 52)), -52);
281 * Select a random double from the normal distribution with
282 * mean = 0.0 and stddev = 1.0.
284 * To get a result from a different normal distribution use
285 * STDDEV * pg_prng_double_normal() + MEAN
287 * Uses https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
297 * pg_prng_double generates [0, 1), but for the basic version of the
298 * Box-Muller transform the two uniformly distributed random numbers are
299 * expected to be in (0, 1]; in particular we'd better not compute log(0).
304 /* Apply Box-Muller transform to get one normal-valued output */
305 z0 = sqrt(-2.0 * log(u1)) * sin(2.0 *
M_PI * u2);
310 * Select a random boolean value.
317 return (
bool) (v >> 63);
static int pg_leftmost_one_pos64(uint64 word)
int64 pg_prng_int64_range(pg_prng_state *state, int64 rmin, int64 rmax)
double pg_prng_double(pg_prng_state *state)
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
int32 pg_prng_int32(pg_prng_state *state)
static uint64 splitmix64(uint64 *state)
uint32 pg_prng_uint32(pg_prng_state *state)
uint64 pg_prng_uint64(pg_prng_state *state)
int32 pg_prng_int32p(pg_prng_state *state)
int64 pg_prng_int64(pg_prng_state *state)
void pg_prng_seed(pg_prng_state *state, uint64 seed)
pg_prng_state pg_global_prng_state
int64 pg_prng_int64p(pg_prng_state *state)
static uint64 rotl(uint64 x, int bits)
bool pg_prng_seed_check(pg_prng_state *state)
double pg_prng_double_normal(pg_prng_state *state)
bool pg_prng_bool(pg_prng_state *state)
void pg_prng_fseed(pg_prng_state *state, double fseed)
static uint64 xoroshiro128ss(pg_prng_state *state)
static struct cvec * range(struct vars *v, chr a, chr b, int cases)