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

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: (view raw, whole thread or download thread mbox)
Lists: pgsql-generalpgsql-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


pgsql-hackers by date

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

pgsql-general by date

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

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