Skip to content

Commit 59bb147

Browse files
committed
Update random() usage so ranges are inclusive/exclusive as required.
1 parent eb7bd06 commit 59bb147

File tree

4 files changed

+10
-17
lines changed

4 files changed

+10
-17
lines changed

src/backend/commands/analyze.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.90 2005/11/22 18:17:08 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.91 2006/02/03 12:45:47 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -927,18 +927,11 @@ acquire_sample_rows(Relation onerel, HeapTuple *rows, int targrows,
927927
return numrows;
928928
}
929929

930-
/* Select a random value R uniformly distributed in 0 < R < 1 */
930+
/* Select a random value R uniformly distributed in (0 - 1) */
931931
static double
932932
random_fract(void)
933933
{
934-
long z;
935-
936-
/* random() can produce endpoint values, try again if so */
937-
do
938-
{
939-
z = random();
940-
} while (z <= 0 || z >= MAX_RANDOM_VALUE);
941-
return (double) z / (double) MAX_RANDOM_VALUE;
934+
return ((double) random() + 1) / ((double) MAX_RANDOM_VALUE + 2);
942935
}
943936

944937
/*

src/backend/storage/lmgr/s_lock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/storage/lmgr/s_lock.c,v 1.41 2005/11/22 18:17:21 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/storage/lmgr/s_lock.c,v 1.42 2006/02/03 12:45:47 momjian Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -120,7 +120,7 @@ s_lock(volatile slock_t *lock, const char *file, int line)
120120

121121
/* increase delay by a random fraction between 1X and 2X */
122122
cur_delay += (int) (cur_delay *
123-
(((double) random()) / ((double) MAX_RANDOM_VALUE)) + 0.5);
123+
((double) random() / (double) MAX_RANDOM_VALUE) + 0.5);
124124
/* wrap back to minimum delay when max is exceeded */
125125
if (cur_delay > MAX_DELAY_MSEC)
126126
cur_delay = MIN_DELAY_MSEC;

src/backend/utils/adt/float.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.119 2005/12/02 02:49:11 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.120 2006/02/03 12:45:47 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1833,8 +1833,8 @@ drandom(PG_FUNCTION_ARGS)
18331833
{
18341834
float8 result;
18351835

1836-
/* result 0.0-1.0 */
1837-
result = ((double) random()) / ((double) MAX_RANDOM_VALUE);
1836+
/* result [0.0 - 1.0) */
1837+
result = (double) random() / ((double) MAX_RANDOM_VALUE + 1);
18381838

18391839
PG_RETURN_FLOAT8(result);
18401840
}

src/include/optimizer/geqo_random.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/optimizer/geqo_random.h,v 1.16 2004/12/31 22:03:36 pgsql Exp $
9+
* $PostgreSQL: pgsql/src/include/optimizer/geqo_random.h,v 1.17 2006/02/03 12:45:47 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -28,7 +28,7 @@
2828

2929
/* geqo_rand returns a random float value between 0 and 1 inclusive */
3030

31-
#define geqo_rand() (((double) random()) / ((double) MAX_RANDOM_VALUE))
31+
#define geqo_rand() ((double) random() / (double) MAX_RANDOM_VALUE)
3232

3333
/* geqo_randint returns integer value between lower and upper inclusive */
3434

0 commit comments

Comments
 (0)