Fix pgbench's getrand() function so that min and max have approximately
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 10 Mar 2008 01:23:04 +0000 (01:23 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 10 Mar 2008 01:23:04 +0000 (01:23 +0000)
the same chance of being selected as do numbers between them.  Problem
noted by Greg Stark; fix by Alexey Klyukin.

contrib/pgbench/pgbench.c

index 340846505b1b10e7935134053620b247e2261837..70daa3ec071a1d56d4e70d5ac22ec4f5e58b4cca 100644 (file)
@@ -191,11 +191,15 @@ usage(void)
        fprintf(stderr, "(initialize mode): pgbench -i [-h hostname][-p port][-s scaling_factor] [-F fillfactor] [-U login][-d][dbname]\n");
 }
 
-/* random number generator */
+/* random number generator: uniform distribution from min to max inclusive */
 static int
 getrand(int min, int max)
 {
-       return min + (int) (((max - min) * (double) random()) / MAX_RANDOM_VALUE + 0.5);
+       /*
+        * Odd coding is so that min and max have approximately the same chance of
+        * being selected as do numbers between them.
+        */
+       return min + (int) (((max - min + 1) * (double) random()) / (MAX_RANDOM_VALUE + 1.0));
 }
 
 /* call PQexec() and exit() on failure */