Skip site navigation (1) Skip section navigation (2)

Re: [HACKERS] Bug: random() can return 1.0

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: andrew(at)supernews(dot)com, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] Bug: random() can return 1.0
Date: 2006-02-01 17:23:54
Message-ID: 200602011723.k11HNsd29134@candle.pha.pa.us (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-patches
Tom Lane wrote:
> Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> > Because random returns a double, I think it is very possible that we
> > could return 1 due to rounding,
> 
> Not unless your machine has a "double" type with less than 32 bits of
> precision, which seems pretty unlikely.  It'd be sufficient to do
> 
> 	/* result 0.0 <= x < 1.0 */
> 	result = ((double) random()) / ((double) MAX_RANDOM_VALUE + 1.0);

Here is a patch that makes this change, and cleans up other
MAX_RANDOM_VALUE uses.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman(at)candle(dot)pha(dot)pa(dot)us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

In response to

pgsql-hackers by date

Next:From: Alvaro HerreraDate: 2006-02-01 17:29:05
Subject: Re: [HACKERS] autovacuum
Previous:From: Chris BrowneDate: 2006-02-01 17:14:41
Subject: Re: autovacuum

pgsql-patches by date

Next:From: Bruce MomjianDate: 2006-02-01 17:45:11
Subject: Re: TODO-Item: B-tree fillfactor control
Previous:From: Bruce MomjianDate: 2006-02-01 15:24:11
Subject: test, please ignore

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group