OSDN Git Service

Update random() usage so ranges are inclusive/exclusive as required.
authorBruce Momjian <bruce@momjian.us>
Fri, 3 Feb 2006 12:45:47 +0000 (12:45 +0000)
committerBruce Momjian <bruce@momjian.us>
Fri, 3 Feb 2006 12:45:47 +0000 (12:45 +0000)
src/backend/commands/analyze.c
src/backend/storage/lmgr/s_lock.c
src/backend/utils/adt/float.c
src/include/optimizer/geqo_random.h

index 095ffe7..06bbe5d 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * 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;
 }
 
-/* Select a random value R uniformly distributed in 0 < R < 1 */
+/* Select a random value R uniformly distributed in (0 - 1) */
 static double
 random_fract(void)
 {
-       long            z;
-
-       /* 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;
+       return ((double) random() + 1) / ((double) MAX_RANDOM_VALUE + 2);
 }
 
 /*
index e0ffcec..774731f 100644 (file)
@@ -9,7 +9,7 @@
  *
  *
  * 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 */
                        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 */
                        if (cur_delay > MAX_DELAY_MSEC)
                                cur_delay = MIN_DELAY_MSEC;
index d50e4ea..488b434 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * 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;
 
-       /* result 0.0-1.0 */
-       result = ((double) random()) / ((double) MAX_RANDOM_VALUE);
+       /* result [0.0 - 1.0) */
+       result = (double) random() / ((double) MAX_RANDOM_VALUE + 1);
 
        PG_RETURN_FLOAT8(result);
 }
index 5d97143..1bc54a6 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * 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 */
 
-#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 */