1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-12 05:01:15 +03:00

Clean up bogosities in use of random(3) and srandom(3) --- do not assume

that RAND_MAX applies to them, since it doesn't.  Instead add a
config.h parameter MAX_RANDOM_VALUE.  This is currently set at 2^31-1
but could be auto-configured if that ever proves necessary.  Also fix
some outright bugs like calling srand() where srandom() is appropriate.
This commit is contained in:
Tom Lane
2000-08-07 00:51:42 +00:00
parent 259489bab7
commit 9426047021
8 changed files with 47 additions and 25 deletions

View File

@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: geqo_main.c,v 1.22 2000/06/28 03:31:45 tgl Exp $
* $Id: geqo_main.c,v 1.23 2000/08/07 00:51:23 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -106,9 +106,9 @@ geqo(Query *root)
/* seed random number generator */
/* XXX why is this done every time around? */
if (Geqo_random_seed >= 0)
srandom(Geqo_random_seed);
srandom((unsigned int) Geqo_random_seed);
else
srandom(time(NULL));
srandom((unsigned int) time(NULL));
/* allocate genetic pool memory */
pool = alloc_pool(pool_size, number_of_rels);

View File

@@ -11,7 +11,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.158 2000/07/28 02:13:26 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.159 2000/08/07 00:51:30 tgl Exp $
*
* NOTES
*
@@ -1850,7 +1850,7 @@ DoBackend(Port *port)
*/
random_seed = 0;
gettimeofday(&now, &tz);
srandom(now.tv_usec);
srandom((unsigned int) now.tv_usec);
/* ----------------
* Now, build the argv vector that will be given to PostgresMain.
@@ -2029,7 +2029,6 @@ RandomSalt(char *salt)
static long
PostmasterRandom(void)
{
static bool initialized = false;
if (!initialized)

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.67 2000/08/01 18:29:35 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.68 2000/08/07 00:51:14 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1315,7 +1315,7 @@ drandom(PG_FUNCTION_ARGS)
float8 result;
/* result 0.0-1.0 */
result = ((double) random()) / RAND_MAX;
result = ((double) random()) / ((double) MAX_RANDOM_VALUE);
PG_RETURN_FLOAT8(result);
}
@@ -1328,7 +1328,7 @@ Datum
setseed(PG_FUNCTION_ARGS)
{
float8 seed = PG_GETARG_FLOAT8(0);
int iseed = (seed * RAND_MAX);
int iseed = (int) (seed * MAX_RANDOM_VALUE);
srandom((unsigned int) iseed);

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.19 2000/06/05 07:28:52 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.20 2000/08/07 00:51:14 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -45,15 +45,18 @@ nonnullvalue(PG_FUNCTION_ARGS)
/*
* oidrand (oid o, int4 X)-
* takes in an oid and a int4 X, and will return 'true'
* about 1/X of the time.
* Takes in an oid and a int4 X, and will return 'true' about 1/X of
* the time. If X == 0, this will always return true.
* Useful for doing random sampling or subsetting.
* if X == 0, this will always return true;
*
* Example use:
* select * from TEMP where oidrand(TEMP.oid, 10)
* will return about 1/10 of the tuples in TEMP
*
* NOTE: the OID input is not used at all. It is there just because of
* an old optimizer bug: a qual expression containing no variables was
* mistakenly assumed to be a constant. Pretending to access the row's OID
* prevented the optimizer from treating the oidrand() result as constant.
*/
static bool random_initialized = false;
@@ -61,7 +64,6 @@ static bool random_initialized = false;
Datum
oidrand(PG_FUNCTION_ARGS)
{
/* XXX seems like we ought to be using the oid for something? */
#ifdef NOT_USED
Oid o = PG_GETARG_OID(0);
#endif
@@ -96,7 +98,7 @@ oidsrand(PG_FUNCTION_ARGS)
{
int32 X = PG_GETARG_INT32(0);
srand(X);
srandom((unsigned int) X);
random_initialized = true;
PG_RETURN_BOOL(true);
}