Re: [GENERAL] random() function produces wrong range - Mailing list pgsql-hackers

From Stephan Szabo
Subject Re: [GENERAL] random() function produces wrong range
Date
Msg-id Pine.BSF.4.10.10008011038390.24330-100000@megazone23.bigpanda.com
Whole thread Raw
In response to random() function produces wrong range  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [GENERAL] random() function produces wrong range  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On Tue, 1 Aug 2000, Tom Lane wrote:

> The comment in the random() function indicates that its author thought
> it'd produce output in the range 0..1, which seems like a pretty
> reasonable definition:
>
>     /* result 0.0-1.0 */
>     result = ((double) random()) / RAND_MAX;
>
> Unfortunately, at least on my box, it produces no such thing.  random()
> actually yields values in the range 0..2^31-1 --- while RAND_MAX is
> only 32767, because it applies to the rand() function not random().

> I would like to propose changing the code to
>
>     /* result 0.0-1.0 */
>     result = ((double) random()) / INT_MAX;
>
> (and making the corresponding change in setseed()).  But I wonder if
> anyone out there has applications that depend on the current behavior.

Actually, on my machines, both man pages for rand() and random() say
they return values between 0 and RAND_MAX (whether that's true or not
is another matter).  In my case RAND_MAX==INT_MAX so the change wouldn't
be a problem, but it might be problematic on some of the 64 bit machines.


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: random() function produces wrong range
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] random() function produces wrong range