PostgreSQL Source Code  git master
pg_prng.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pg_prng_state
 

Macros

#define pg_prng_strong_seed(state)
 

Typedefs

typedef struct pg_prng_state pg_prng_state
 

Functions

void pg_prng_seed (pg_prng_state *state, uint64 seed)
 
void pg_prng_fseed (pg_prng_state *state, double fseed)
 
bool pg_prng_seed_check (pg_prng_state *state)
 
uint64 pg_prng_uint64 (pg_prng_state *state)
 
uint64 pg_prng_uint64_range (pg_prng_state *state, uint64 rmin, uint64 rmax)
 
int64 pg_prng_int64 (pg_prng_state *state)
 
int64 pg_prng_int64p (pg_prng_state *state)
 
int64 pg_prng_int64_range (pg_prng_state *state, int64 rmin, int64 rmax)
 
uint32 pg_prng_uint32 (pg_prng_state *state)
 
int32 pg_prng_int32 (pg_prng_state *state)
 
int32 pg_prng_int32p (pg_prng_state *state)
 
double pg_prng_double (pg_prng_state *state)
 
double pg_prng_double_normal (pg_prng_state *state)
 
bool pg_prng_bool (pg_prng_state *state)
 

Variables

PGDLLIMPORT pg_prng_state pg_global_prng_state
 

Macro Definition Documentation

◆ pg_prng_strong_seed

#define pg_prng_strong_seed (   state)
Value:
pg_prng_seed_check(state) : false)
return false
Definition: isn.c:130
bool pg_strong_random(void *buf, size_t len)
Definition: regguts.h:323

Definition at line 46 of file pg_prng.h.

Typedef Documentation

◆ pg_prng_state

typedef struct pg_prng_state pg_prng_state

Function Documentation

◆ pg_prng_bool()

bool pg_prng_bool ( pg_prng_state state)

Definition at line 313 of file pg_prng.c.

314 {
316 
317  return (bool) (v >> 63);
318 }
uint64_t uint64
Definition: c.h:486
static uint64 xoroshiro128ss(pg_prng_state *state)
Definition: pg_prng.c:54

References xoroshiro128ss().

Referenced by gistchoose().

◆ pg_prng_double()

double pg_prng_double ( pg_prng_state state)

Definition at line 268 of file pg_prng.c.

269 {
271 
272  /*
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.
276  */
277  return ldexp((double) (v >> (64 - 52)), -52);
278 }

References xoroshiro128ss().

Referenced by check_log_duration(), computeIterativeZipfian(), doLog(), drandom(), explain_ExecutorStart(), geqo_rand(), get_normal_pair(), getExponentialRand(), getPoissonRand(), perform_spin_delay(), pg_prng_double_normal(), sampler_random_fract(), and StartTransaction().

◆ pg_prng_double_normal()

double pg_prng_double_normal ( pg_prng_state state)

Definition at line 290 of file pg_prng.c.

291 {
292  double u1,
293  u2,
294  z0;
295 
296  /*
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).
300  */
301  u1 = 1.0 - pg_prng_double(state);
302  u2 = 1.0 - pg_prng_double(state);
303 
304  /* Apply Box-Muller transform to get one normal-valued output */
305  z0 = sqrt(-2.0 * log(u1)) * sin(2.0 * M_PI * u2);
306  return z0;
307 }
double pg_prng_double(pg_prng_state *state)
Definition: pg_prng.c:268
#define M_PI
Definition: pg_prng.c:29

References M_PI, and pg_prng_double().

Referenced by drandom_normal(), and getGaussianRand().

◆ pg_prng_fseed()

void pg_prng_fseed ( pg_prng_state state,
double  fseed 
)

Definition at line 102 of file pg_prng.c.

103 {
104  /* Assume there's about 52 mantissa bits; the sign contributes too. */
105  int64 seed = ((double) ((UINT64CONST(1) << 52) - 1)) * fseed;
106 
107  pg_prng_seed(state, (uint64) seed);
108 }
int64_t int64
Definition: c.h:482
#define UINT64CONST(x)
Definition: c.h:500
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:89

References pg_prng_seed(), and UINT64CONST.

Referenced by geqo_set_seed(), and setseed().

◆ pg_prng_int32()

int32 pg_prng_int32 ( pg_prng_state state)

Definition at line 243 of file pg_prng.c.

244 {
246 
247  return (int32) (v >> 32);
248 }
int32_t int32
Definition: c.h:481

References xoroshiro128ss().

Referenced by prepare_buf().

◆ pg_prng_int32p()

int32 pg_prng_int32p ( pg_prng_state state)

Definition at line 254 of file pg_prng.c.

255 {
257 
258  return (int32) (v >> 33);
259 }

References xoroshiro128ss().

Referenced by create_and_test_bloom().

◆ pg_prng_int64()

int64 pg_prng_int64 ( pg_prng_state state)

Definition at line 173 of file pg_prng.c.

174 {
175  return (int64) xoroshiro128ss(state);
176 }

References xoroshiro128ss().

◆ pg_prng_int64_range()

int64 pg_prng_int64_range ( pg_prng_state state,
int64  rmin,
int64  rmax 
)

Definition at line 192 of file pg_prng.c.

193 {
194  int64 val;
195 
196  if (likely(rmax > rmin))
197  {
198  uint64 uval;
199 
200  /*
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.
203  */
204  uval = (uint64) rmin +
205  pg_prng_uint64_range(state, 0, (uint64) rmax - (uint64) rmin);
206 
207  /*
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.
211  */
212  if (uval > PG_INT64_MAX)
213  val = (int64) (uval - PG_INT64_MIN) + PG_INT64_MIN;
214  else
215  val = (int64) uval;
216  }
217  else
218  val = rmin;
219 
220  return val;
221 }
#define likely(x)
Definition: c.h:329
#define PG_INT64_MAX
Definition: c.h:546
#define PG_INT64_MIN
Definition: c.h:545
long val
Definition: informix.c:689
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
Definition: pg_prng.c:144

References likely, PG_INT64_MAX, PG_INT64_MIN, pg_prng_uint64_range(), and val.

Referenced by int4random(), and int8random().

◆ pg_prng_int64p()

int64 pg_prng_int64p ( pg_prng_state state)

Definition at line 182 of file pg_prng.c.

183 {
184  return (int64) (xoroshiro128ss(state) & UINT64CONST(0x7FFFFFFFFFFFFFFF));
185 }

References UINT64CONST, and xoroshiro128ss().

◆ pg_prng_seed()

void pg_prng_seed ( pg_prng_state state,
uint64  seed 
)

Definition at line 89 of file pg_prng.c.

90 {
91  state->s0 = splitmix64(&seed);
92  state->s1 = splitmix64(&seed);
93  /* Let's just make sure we didn't get all-zeroes */
94  (void) pg_prng_seed_check(state);
95 }
static uint64 splitmix64(uint64 *state)
Definition: pg_prng.c:72
bool pg_prng_seed_check(pg_prng_state *state)
Definition: pg_prng.c:114

References pg_prng_seed_check(), and splitmix64().

Referenced by choose_dsm_implementation(), initialize_prng(), InitProcessGlobals(), initRandomState(), libpq_prng_init(), main(), permute(), pg_prng_fseed(), sampler_random_init_state(), set_random_seed(), setup_publisher(), and test_random().

◆ pg_prng_seed_check()

bool pg_prng_seed_check ( pg_prng_state state)

Definition at line 114 of file pg_prng.c.

115 {
116  /*
117  * If the seeding mechanism chanced to produce all-zeroes, insert
118  * something nonzero. Anything would do; use Knuth's LCG parameters.
119  */
120  if (unlikely(state->s0 == 0 && state->s1 == 0))
121  {
122  state->s0 = UINT64CONST(0x5851F42D4C957F2D);
123  state->s1 = UINT64CONST(0x14057B7EF767814F);
124  }
125 
126  /* As a convenience for the pg_prng_strong_seed macro, return true */
127  return true;
128 }
#define unlikely(x)
Definition: c.h:330

References UINT64CONST, and unlikely.

Referenced by pg_prng_seed().

◆ pg_prng_uint32()

uint32 pg_prng_uint32 ( pg_prng_state state)

Definition at line 227 of file pg_prng.c.

228 {
229  /*
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.
233  */
235 
236  return (uint32) (v >> 32);
237 }
uint32_t uint32
Definition: c.h:485

References xoroshiro128ss().

Referenced by _bt_findinsertloc(), acquire_sample_rows(), anl_init_selection_state(), anl_random_fract(), CatalogCacheCreateEntry(), choose_dsm_implementation(), dsm_create(), dsm_postmaster_startup(), ExecInitSampleScan(), generate_object_name(), InitProcessGlobals(), make_main_region_dsm_handle(), reservoir_init_selection_state(), and test_huge_distances().

◆ pg_prng_uint64()

uint64 pg_prng_uint64 ( pg_prng_state state)

Definition at line 134 of file pg_prng.c.

135 {
136  return xoroshiro128ss(state);
137 }

References xoroshiro128ss().

Referenced by bt_check_every_level(), initRandomState(), main(), permute(), and test_random().

◆ pg_prng_uint64_range()

uint64 pg_prng_uint64_range ( pg_prng_state state,
uint64  rmin,
uint64  rmax 
)

Definition at line 144 of file pg_prng.c.

145 {
146  uint64 val;
147 
148  if (likely(rmax > rmin))
149  {
150  /*
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.
154  */
155  uint64 range = rmax - rmin;
156  uint32 rshift = 63 - pg_leftmost_one_pos64(range);
157 
158  do
159  {
160  val = xoroshiro128ss(state) >> rshift;
161  } while (val > range);
162  }
163  else
164  val = 0;
165 
166  return rmin + val;
167 }
static int pg_leftmost_one_pos64(uint64 word)
Definition: pg_bitutils.h:72
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412

References likely, pg_leftmost_one_pos64(), range(), val, and xoroshiro128ss().

Referenced by array_shuffle_n(), geqo_randint(), GetPermutation(), getrand(), permute(), pg_prng_int64_range(), pqConnectOptions2(), PQconnectPoll(), random_var(), SetTempTablespaces(), spgdoinsert(), test_pattern(), testdelete(), and testfindltgt().

Variable Documentation

◆ pg_global_prng_state