Silly bug in pgbench's random number generator

Started by Bruce Momjianabout 19 years ago6 messagespatches
Jump to latest
#1Bruce Momjian
bruce@momjian.us

pgbench's random number generator was only generating the first and last value
in the specified range half as often as other values in the range. Not that it
actually matters but it may as well do what it claims. This line has a pretty
long and sordid history with various people tweaking it one way and another.

cvs diff: Diffing contrib/pgbench
Index: contrib/pgbench/pgbench.c
===================================================================
RCS file: /home/stark/src/REPOSITORY/pgsql/contrib/pgbench/pgbench.c,v
retrieving revision 1.66
diff -u -r1.66 pgbench.c
--- contrib/pgbench/pgbench.c	24 May 2007 18:54:10 -0000	1.66
+++ contrib/pgbench/pgbench.c	14 Jun 2007 16:22:19 -0000
@@ -191,7 +191,7 @@
 static int
 getrand(int min, int max)
 {
-	return min + (int) (((max - min) * (double) random()) / MAX_RANDOM_VALUE + 0.5);
+	return min + (int) (((max - min + 1) * (double) random()) / MAX_RANDOM_VALUE);
 }

/* call PQexec() and exit() on failure */

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

#2Oleksii Kliukin
alexk@hintbits.com
In reply to: Bruce Momjian (#1)
Re: Silly bug in pgbench's random number generator

Gregory Stark wrote:

pgbench's random number generator was only generating the first and last value
in the specified range half as often as other values in the range. Not that it
actually matters but it may as well do what it claims. This line has a pretty
long and sordid history with various people tweaking it one way and another.

cvs diff: Diffing contrib/pgbench
Index: contrib/pgbench/pgbench.c
===================================================================
RCS file: /home/stark/src/REPOSITORY/pgsql/contrib/pgbench/pgbench.c,v
retrieving revision 1.66
diff -u -r1.66 pgbench.c
--- contrib/pgbench/pgbench.c	24 May 2007 18:54:10 -0000	1.66
+++ contrib/pgbench/pgbench.c	14 Jun 2007 16:22:19 -0000
@@ -191,7 +191,7 @@
static int
getrand(int min, int max)
{
-	return min + (int) (((max - min) * (double) random()) / MAX_RANDOM_VALUE + 0.5);
+	return min + (int) (((max - min + 1) * (double) random()) / MAX_RANDOM_VALUE);
}

/* call PQexec() and exit() on failure */

I think this line should be altered this way:
return min + (int) (((max - min + 1) * (double) random()) / (MAX_RANDOM_VALUE + 1.0));
eliminating the result of max + 1 in a corner case when random() equals to
MAX_RANDOM_VALUE.

--
Alexey Klyukin http://www.commandprompt.com/
The PostgreSQL Company - Command Prompt, Inc.

#3Bruce Momjian
bruce@momjian.us
In reply to: Bruce Momjian (#1)
Re: Silly bug in pgbench's random number generator

This has been saved for the 8.4 release:

http://momjian.postgresql.org/cgi-bin/pgpatches_hold

---------------------------------------------------------------------------

Gregory Stark wrote:

pgbench's random number generator was only generating the first and last value
in the specified range half as often as other values in the range. Not that it
actually matters but it may as well do what it claims. This line has a pretty
long and sordid history with various people tweaking it one way and another.

cvs diff: Diffing contrib/pgbench
Index: contrib/pgbench/pgbench.c
===================================================================
RCS file: /home/stark/src/REPOSITORY/pgsql/contrib/pgbench/pgbench.c,v
retrieving revision 1.66
diff -u -r1.66 pgbench.c
--- contrib/pgbench/pgbench.c	24 May 2007 18:54:10 -0000	1.66
+++ contrib/pgbench/pgbench.c	14 Jun 2007 16:22:19 -0000
@@ -191,7 +191,7 @@
static int
getrand(int min, int max)
{
-	return min + (int) (((max - min) * (double) random()) / MAX_RANDOM_VALUE + 0.5);
+	return min + (int) (((max - min + 1) * (double) random()) / MAX_RANDOM_VALUE);
}

/* call PQexec() and exit() on failure */

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

#4Bruce Momjian
bruce@momjian.us
In reply to: Bruce Momjian (#3)
Re: Silly bug in pgbench's random number generator

Uhm, this is a one-line *bug fix*.

"Bruce Momjian" <bruce@momjian.us> writes:

This has been saved for the 8.4 release:

http://momjian.postgresql.org/cgi-bin/pgpatches_hold

---------------------------------------------------------------------------

Gregory Stark wrote:

pgbench's random number generator was only generating the first and last value
in the specified range half as often as other values in the range. Not that it
actually matters but it may as well do what it claims. This line has a pretty
long and sordid history with various people tweaking it one way and another.

cvs diff: Diffing contrib/pgbench
Index: contrib/pgbench/pgbench.c
===================================================================
RCS file: /home/stark/src/REPOSITORY/pgsql/contrib/pgbench/pgbench.c,v
retrieving revision 1.66
diff -u -r1.66 pgbench.c
--- contrib/pgbench/pgbench.c	24 May 2007 18:54:10 -0000	1.66
+++ contrib/pgbench/pgbench.c	14 Jun 2007 16:22:19 -0000
@@ -191,7 +191,7 @@
static int
getrand(int min, int max)
{
-	return min + (int) (((max - min) * (double) random()) / MAX_RANDOM_VALUE + 0.5);
+	return min + (int) (((max - min + 1) * (double) random()) / MAX_RANDOM_VALUE);
}

/* call PQexec() and exit() on failure */

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

#5Bruce Momjian
bruce@momjian.us
In reply to: Bruce Momjian (#4)
Re: Silly bug in pgbench's random number generator

Gregory Stark wrote:

Uhm, this is a one-line *bug fix*.

There wasn't agreement on how it should be fixed, it is a minor bug, and
as you said, we should be focusing on the significant outstanding
patches submitted before feature freeze.

---------------------------------------------------------------------------

"Bruce Momjian" <bruce@momjian.us> writes:

This has been saved for the 8.4 release:

http://momjian.postgresql.org/cgi-bin/pgpatches_hold

---------------------------------------------------------------------------

Gregory Stark wrote:

pgbench's random number generator was only generating the first and last value
in the specified range half as often as other values in the range. Not that it
actually matters but it may as well do what it claims. This line has a pretty
long and sordid history with various people tweaking it one way and another.

cvs diff: Diffing contrib/pgbench
Index: contrib/pgbench/pgbench.c
===================================================================
RCS file: /home/stark/src/REPOSITORY/pgsql/contrib/pgbench/pgbench.c,v
retrieving revision 1.66
diff -u -r1.66 pgbench.c
--- contrib/pgbench/pgbench.c	24 May 2007 18:54:10 -0000	1.66
+++ contrib/pgbench/pgbench.c	14 Jun 2007 16:22:19 -0000
@@ -191,7 +191,7 @@
static int
getrand(int min, int max)
{
-	return min + (int) (((max - min) * (double) random()) / MAX_RANDOM_VALUE + 0.5);
+	return min + (int) (((max - min + 1) * (double) random()) / MAX_RANDOM_VALUE);
}

/* call PQexec() and exit() on failure */

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Oleksii Kliukin (#2)
Re: Silly bug in pgbench's random number generator

Alexey Klyukin <alexk@commandprompt.com> writes:

Gregory Stark wrote:

pgbench's random number generator was only generating the first and last value
in the specified range half as often as other values in the range.

I think this line should be altered this way:
return min + (int) (((max - min + 1) * (double) random()) / (MAX_RANDOM_VALUE + 1.0));
eliminating the result of max + 1 in a corner case when random() equals to
MAX_RANDOM_VALUE.

Yeah, that looks more correct. Applied.

regards, tom lane