PostgreSQL Source Code  git master
pseudorandomfuncs.c File Reference
#include "postgres.h"
#include <math.h>
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "utils/fmgrprotos.h"
#include "utils/numeric.h"
#include "utils/timestamp.h"
Include dependency graph for pseudorandomfuncs.c:

Go to the source code of this file.

Functions

static void initialize_prng (void)
 
Datum setseed (PG_FUNCTION_ARGS)
 
Datum drandom (PG_FUNCTION_ARGS)
 
Datum drandom_normal (PG_FUNCTION_ARGS)
 
Datum int4random (PG_FUNCTION_ARGS)
 
Datum int8random (PG_FUNCTION_ARGS)
 
Datum numeric_random (PG_FUNCTION_ARGS)
 

Variables

static pg_prng_state prng_state
 
static bool prng_seed_set = false
 

Function Documentation

◆ drandom()

Datum drandom ( PG_FUNCTION_ARGS  )

Definition at line 84 of file pseudorandomfuncs.c.

85 {
86  float8 result;
87 
89 
90  /* pg_prng_double produces desired result range [0.0, 1.0) */
91  result = pg_prng_double(&prng_state);
92 
93  PG_RETURN_FLOAT8(result);
94 }
double float8
Definition: c.h:630
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
double pg_prng_double(pg_prng_state *state)
Definition: pg_prng.c:268
static void initialize_prng(void)
static pg_prng_state prng_state

References initialize_prng(), pg_prng_double(), PG_RETURN_FLOAT8, and prng_state.

◆ drandom_normal()

Datum drandom_normal ( PG_FUNCTION_ARGS  )

Definition at line 102 of file pseudorandomfuncs.c.

103 {
104  float8 mean = PG_GETARG_FLOAT8(0);
105  float8 stddev = PG_GETARG_FLOAT8(1);
106  float8 result,
107  z;
108 
109  initialize_prng();
110 
111  /* Get random value from standard normal(mean = 0.0, stddev = 1.0) */
113  /* Transform the normal standard variable (z) */
114  /* using the target normal distribution parameters */
115  result = (stddev * z) + mean;
116 
117  PG_RETURN_FLOAT8(result);
118 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
double pg_prng_double_normal(pg_prng_state *state)
Definition: pg_prng.c:290

References initialize_prng(), PG_GETARG_FLOAT8, pg_prng_double_normal(), PG_RETURN_FLOAT8, and prng_state.

◆ initialize_prng()

static void initialize_prng ( void  )
static

Definition at line 34 of file pseudorandomfuncs.c.

35 {
36  if (unlikely(!prng_seed_set))
37  {
38  /*
39  * If possible, seed the PRNG using high-quality random bits. Should
40  * that fail for some reason, we fall back on a lower-quality seed
41  * based on current time and PID.
42  */
44  {
46  uint64 iseed;
47 
48  /* Mix the PID with the most predictable bits of the timestamp */
49  iseed = (uint64) now ^ ((uint64) MyProcPid << 32);
50  pg_prng_seed(&prng_state, iseed);
51  }
52  prng_seed_set = true;
53  }
54 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1654
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1618
#define unlikely(x)
Definition: c.h:311
int64 TimestampTz
Definition: timestamp.h:39
int MyProcPid
Definition: globals.c:45
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:89
#define pg_prng_strong_seed(state)
Definition: pg_prng.h:46
static bool prng_seed_set

References GetCurrentTimestamp(), MyProcPid, now(), pg_prng_seed(), pg_prng_strong_seed, prng_seed_set, prng_state, and unlikely.

Referenced by drandom(), drandom_normal(), int4random(), int8random(), and numeric_random().

◆ int4random()

Datum int4random ( PG_FUNCTION_ARGS  )

Definition at line 126 of file pseudorandomfuncs.c.

127 {
128  int32 rmin = PG_GETARG_INT32(0);
129  int32 rmax = PG_GETARG_INT32(1);
130  int32 result;
131 
132  if (rmin > rmax)
133  ereport(ERROR,
134  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
135  errmsg("lower bound must be less than or equal to upper bound"));
136 
137  initialize_prng();
138 
139  result = (int32) pg_prng_int64_range(&prng_state, rmin, rmax);
140 
141  PG_RETURN_INT32(result);
142 }
signed int int32
Definition: c.h:494
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int64 pg_prng_int64_range(pg_prng_state *state, int64 rmin, int64 rmax)
Definition: pg_prng.c:192

References ereport, errcode(), errmsg(), ERROR, initialize_prng(), PG_GETARG_INT32, pg_prng_int64_range(), PG_RETURN_INT32, and prng_state.

◆ int8random()

Datum int8random ( PG_FUNCTION_ARGS  )

Definition at line 150 of file pseudorandomfuncs.c.

151 {
152  int64 rmin = PG_GETARG_INT64(0);
153  int64 rmax = PG_GETARG_INT64(1);
154  int64 result;
155 
156  if (rmin > rmax)
157  ereport(ERROR,
158  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
159  errmsg("lower bound must be less than or equal to upper bound"));
160 
161  initialize_prng();
162 
163  result = pg_prng_int64_range(&prng_state, rmin, rmax);
164 
165  PG_RETURN_INT64(result);
166 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283

References ereport, errcode(), errmsg(), ERROR, initialize_prng(), PG_GETARG_INT64, pg_prng_int64_range(), PG_RETURN_INT64, and prng_state.

◆ numeric_random()

Datum numeric_random ( PG_FUNCTION_ARGS  )

Definition at line 174 of file pseudorandomfuncs.c.

175 {
176  Numeric rmin = PG_GETARG_NUMERIC(0);
177  Numeric rmax = PG_GETARG_NUMERIC(1);
178  Numeric result;
179 
180  initialize_prng();
181 
182  result = random_numeric(&prng_state, rmin, rmax);
183 
184  PG_RETURN_NUMERIC(result);
185 }
Numeric random_numeric(pg_prng_state *state, Numeric rmin, Numeric rmax)
Definition: numeric.c:4228
#define PG_GETARG_NUMERIC(n)
Definition: numeric.h:78
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:80

References initialize_prng(), PG_GETARG_NUMERIC, PG_RETURN_NUMERIC, prng_state, and random_numeric().

◆ setseed()

Datum setseed ( PG_FUNCTION_ARGS  )

Definition at line 62 of file pseudorandomfuncs.c.

63 {
64  float8 seed = PG_GETARG_FLOAT8(0);
65 
66  if (seed < -1 || seed > 1 || isnan(seed))
67  ereport(ERROR,
68  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
69  errmsg("setseed parameter %g is out of allowed range [-1,1]",
70  seed));
71 
72  pg_prng_fseed(&prng_state, seed);
73  prng_seed_set = true;
74 
76 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void pg_prng_fseed(pg_prng_state *state, double fseed)
Definition: pg_prng.c:102

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_FLOAT8, pg_prng_fseed(), PG_RETURN_VOID, prng_seed_set, and prng_state.

Referenced by assign_random_seed().

Variable Documentation

◆ prng_seed_set

bool prng_seed_set = false
static

Definition at line 26 of file pseudorandomfuncs.c.

Referenced by initialize_prng(), and setseed().

◆ prng_state