Re: Silly bug in pgbench's random number generator

From: Alexey Klyukin <alexk(at)commandprompt(dot)com>
To: Gregory Stark <stark(at)enterprisedb(dot)com>
Cc: pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: Silly bug in pgbench's random number generator
Date: 2007-06-14 19:19:08
Message-ID: 20070614191908.GA2317@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

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.

In response to

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Gregory Maxwell 2007-06-15 02:37:14 Re: Sorted writes in checkpoint
Previous Message Simon Riggs 2007-06-14 17:50:17 Re: Sorted writes in checkpoint