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

random() function produces wrong range

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org, pgsql-general(at)postgreSQL(dot)org
Subject: random() function produces wrong range
Date: 2000-08-01 16:02:36
Message-ID: 22061.965145756@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-generalpgsql-hackers
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().
So what I actually get is floating-point output in the range 0..65535.

regression=# select random();
      random
------------------
 35771.3981139561
(1 row)

regression=# select random();
      random
------------------
 58647.5821405683
(1 row)

This is, to say the least, a bizarre definition.

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.

As far as I can find, random() isn't mentioned in the documentation
currently, so there probably aren't many people using it...

			regards, tom lane

Responses

pgsql-hackers by date

Next:From: Philip WarnerDate: 2000-08-01 16:10:49
Subject: Re: pg_dump and pg_restore, speed
Previous:From: Lamar OwenDate: 2000-08-01 15:45:58
Subject: Re: RPMs built for Mandrake

pgsql-general by date

Next:From: Stephan SzaboDate: 2000-08-01 16:51:35
Subject: Re: How to alter the size of a column
Previous:From: Fetter, David MDate: 2000-08-01 14:40:12
Subject: RE: postgres perl DBI

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