1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-22 23:02:54 +03:00

Update random() usage so ranges are inclusive/exclusive as required.

This commit is contained in:
Bruce Momjian 2006-02-03 12:45:47 +00:00
parent eb7bd06983
commit 59bb147353
4 changed files with 10 additions and 17 deletions

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.90 2005/11/22 18:17:08 momjian Exp $ * $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.91 2006/02/03 12:45:47 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -927,18 +927,11 @@ acquire_sample_rows(Relation onerel, HeapTuple *rows, int targrows,
return numrows; return numrows;
} }
/* Select a random value R uniformly distributed in 0 < R < 1 */ /* Select a random value R uniformly distributed in (0 - 1) */
static double static double
random_fract(void) random_fract(void)
{ {
long z; return ((double) random() + 1) / ((double) MAX_RANDOM_VALUE + 2);
/* random() can produce endpoint values, try again if so */
do
{
z = random();
} while (z <= 0 || z >= MAX_RANDOM_VALUE);
return (double) z / (double) MAX_RANDOM_VALUE;
} }
/* /*

View File

@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/lmgr/s_lock.c,v 1.41 2005/11/22 18:17:21 momjian Exp $ * $PostgreSQL: pgsql/src/backend/storage/lmgr/s_lock.c,v 1.42 2006/02/03 12:45:47 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -120,7 +120,7 @@ s_lock(volatile slock_t *lock, const char *file, int line)
/* increase delay by a random fraction between 1X and 2X */ /* increase delay by a random fraction between 1X and 2X */
cur_delay += (int) (cur_delay * cur_delay += (int) (cur_delay *
(((double) random()) / ((double) MAX_RANDOM_VALUE)) + 0.5); ((double) random() / (double) MAX_RANDOM_VALUE) + 0.5);
/* wrap back to minimum delay when max is exceeded */ /* wrap back to minimum delay when max is exceeded */
if (cur_delay > MAX_DELAY_MSEC) if (cur_delay > MAX_DELAY_MSEC)
cur_delay = MIN_DELAY_MSEC; cur_delay = MIN_DELAY_MSEC;

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.119 2005/12/02 02:49:11 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.120 2006/02/03 12:45:47 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1833,8 +1833,8 @@ drandom(PG_FUNCTION_ARGS)
{ {
float8 result; float8 result;
/* result 0.0-1.0 */ /* result [0.0 - 1.0) */
result = ((double) random()) / ((double) MAX_RANDOM_VALUE); result = (double) random() / ((double) MAX_RANDOM_VALUE + 1);
PG_RETURN_FLOAT8(result); PG_RETURN_FLOAT8(result);
} }

View File

@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/optimizer/geqo_random.h,v 1.16 2004/12/31 22:03:36 pgsql Exp $ * $PostgreSQL: pgsql/src/include/optimizer/geqo_random.h,v 1.17 2006/02/03 12:45:47 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -28,7 +28,7 @@
/* geqo_rand returns a random float value between 0 and 1 inclusive */ /* geqo_rand returns a random float value between 0 and 1 inclusive */
#define geqo_rand() (((double) random()) / ((double) MAX_RANDOM_VALUE)) #define geqo_rand() ((double) random() / (double) MAX_RANDOM_VALUE)
/* geqo_randint returns integer value between lower and upper inclusive */ /* geqo_randint returns integer value between lower and upper inclusive */