/* Shared PRNG state used by all the random functions */ static pg_prng_state prng_state; staticbool prng_seed_set = false;
/* *initialize_prng()- * *Initialize(seed)thePRNG,ifnotdoneyetinthisprocess.
*/ staticvoid
initialize_prng(void)
{ if (unlikely(!prng_seed_set))
{ /* *Ifpossible,seedthePRNGusinghigh-qualityrandombits.Should *thatfailforsomereason,wefallbackonalower-qualityseed *basedoncurrenttimeandPID.
*/ if (unlikely(!pg_prng_strong_seed(&prng_state)))
{
TimestampTz now = GetCurrentTimestamp();
uint64 iseed;
/* Mix the PID with the most predictable bits of the timestamp */
iseed = (uint64) now ^ ((uint64) MyProcPid << 32);
pg_prng_seed(&prng_state, iseed);
}
prng_seed_set = true;
}
}
if (seed < -1 || seed > 1 || isnan(seed))
ereport(ERROR,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("setseed parameter %g is out of allowed range [-1,1]",
seed));
/* *drandom()- * *Returnsarandomnumberchosenuniformlyintherange[0.0,1.0).
*/
Datum
drandom(PG_FUNCTION_ARGS)
{
float8 result;
initialize_prng();
/* pg_prng_double produces desired result range [0.0, 1.0) */
result = pg_prng_double(&prng_state);
PG_RETURN_FLOAT8(result);
}
/* *drandom_normal()- * *Returnsarandomnumberfromanormaldistribution.
*/
Datum
drandom_normal(PG_FUNCTION_ARGS)
{
float8 mean = PG_GETARG_FLOAT8(0);
float8 stddev = PG_GETARG_FLOAT8(1);
float8 result,
z;
initialize_prng();
/* Get random value from standard normal(mean = 0.0, stddev = 1.0) */
z = pg_prng_double_normal(&prng_state); /* Transform the normal standard variable (z) */ /* using the target normal distribution parameters */
result = (stddev * z) + mean;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.