PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_prng.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * Pseudo-Random Number Generator
4 *
5 * Copyright (c) 2021-2024, PostgreSQL Global Development Group
6 *
7 * src/include/common/pg_prng.h
8 *
9 *-------------------------------------------------------------------------
10 */
11#ifndef PG_PRNG_H
12#define PG_PRNG_H
13
14/*
15 * State vector for PRNG generation. Callers should treat this as an
16 * opaque typedef, but we expose its definition to allow it to be
17 * embedded in other structs.
18 */
19typedef struct pg_prng_state
20{
24
25/*
26 * Callers not needing local PRNG series may use this global state vector,
27 * after initializing it with one of the pg_prng_...seed functions.
28 */
30
31extern void pg_prng_seed(pg_prng_state *state, uint64 seed);
32extern void pg_prng_fseed(pg_prng_state *state, double fseed);
34
35/*
36 * Initialize the PRNG state from the pg_strong_random source,
37 * taking care that we don't produce all-zeroes. If this returns false,
38 * caller should initialize the PRNG state from some other random seed,
39 * using pg_prng_[f]seed.
40 *
41 * We implement this as a macro, so that the pg_strong_random() call is
42 * in the caller. If it were in pg_prng.c, programs using pg_prng.c
43 * but not needing strong seeding would nonetheless be forced to pull in
44 * pg_strong_random.c and thence OpenSSL.
45 */
46#define pg_prng_strong_seed(state) \
47 (pg_strong_random(state, sizeof(pg_prng_state)) ? \
48 pg_prng_seed_check(state) : false)
49
58extern double pg_prng_double(pg_prng_state *state);
60extern bool pg_prng_bool(pg_prng_state *state);
61
62#endif /* PG_PRNG_H */
#define PGDLLIMPORT
Definition: c.h:1274
int64_t int64
Definition: c.h:482
int32_t int32
Definition: c.h:481
uint64_t uint64
Definition: c.h:486
uint32_t uint32
Definition: c.h:485
int64 pg_prng_int64_range(pg_prng_state *state, int64 rmin, int64 rmax)
Definition: pg_prng.c:192
double pg_prng_double(pg_prng_state *state)
Definition: pg_prng.c:268
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
Definition: pg_prng.c:144
int32 pg_prng_int32(pg_prng_state *state)
Definition: pg_prng.c:243
uint32 pg_prng_uint32(pg_prng_state *state)
Definition: pg_prng.c:227
uint64 pg_prng_uint64(pg_prng_state *state)
Definition: pg_prng.c:134
int32 pg_prng_int32p(pg_prng_state *state)
Definition: pg_prng.c:254
int64 pg_prng_int64(pg_prng_state *state)
Definition: pg_prng.c:173
struct pg_prng_state pg_prng_state
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:89
int64 pg_prng_int64p(pg_prng_state *state)
Definition: pg_prng.c:182
bool pg_prng_seed_check(pg_prng_state *state)
Definition: pg_prng.c:114
PGDLLIMPORT pg_prng_state pg_global_prng_state
Definition: pg_prng.c:34
double pg_prng_double_normal(pg_prng_state *state)
Definition: pg_prng.c:290
bool pg_prng_bool(pg_prng_state *state)
Definition: pg_prng.c:313
void pg_prng_fseed(pg_prng_state *state, double fseed)
Definition: pg_prng.c:102
uint64 s0
Definition: pg_prng.h:21
uint64 s1
Definition: pg_prng.h:22
Definition: regguts.h:323