Re: Bug: random() can return 1.0

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: andrew(at)supernews(dot)com, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug: random() can return 1.0
Date: 2006-01-19 23:26:09
Message-ID: 10560.1137713169@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

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);

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2006-01-19 23:38:11 Re: Large Scale Aggregation (HashAgg Enhancement)
Previous Message Simon Riggs 2006-01-19 23:20:54 Re: Large Scale Aggregation (HashAgg Enhancement)

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2006-01-19 23:28:06 Re: [HACKERS] pgxs/windows
Previous Message Bruce Momjian 2006-01-19 23:10:17 Re: [PATCHES] Example for UPDATE FROM with correllation