summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2008-03-10 01:23:04 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2008-03-10 01:23:04 +0000
commit9537739f7fcd3bafa2471b199297ec7ad7c935e7 (patch)
tree4282364b9527cdbb56bd16d68e21696ae71615da
parentd9384a4b7334ba755db88160fb96cd33725bd6d3 (diff)
Fix pgbench's getrand() function so that min and max have approximately
the same chance of being selected as do numbers between them. Problem noted by Greg Stark; fix by Alexey Klyukin.
-rw-r--r--contrib/pgbench/pgbench.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c
index ce35bd3dff5..b5082e0abd6 100644
--- a/contrib/pgbench/pgbench.c
+++ b/contrib/pgbench/pgbench.c
@@ -1,5 +1,5 @@
/*
- * $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.75 2007/12/11 02:31:49 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.76 2008/03/10 01:23:04 tgl Exp $
*
* pgbench: a simple benchmark program for PostgreSQL
* written by Tatsuo Ishii
@@ -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 */