Re: random() function produces wrong range

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgreSQL(dot)org, pgsql-general(at)postgreSQL(dot)org
Subject: Re: random() function produces wrong range
Date: 2000-08-01 17:46:46
Message-ID: Pine.BSF.4.10.10008011038390.24330-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general 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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Fetter, David M 2000-08-01 18:23:13 RE: Postgres connect with Access
Previous Message Bruce Momjian 2000-08-01 17:42:59 Re: auto rollback

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2000-08-01 18:23:38 Re: random() function produces wrong range
Previous Message Thomas Swan 2000-08-01 17:27:35 Re: RPMs built for Mandrake