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

Re: [HACKERS] random() function produces wrong range

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Malcolm Beattie <mbeattie(at)sable(dot)ox(dot)ac(dot)uk>
Cc: Thomas Lockhart <lockhart(at)alumni(dot)caltech(dot)edu>, pgsql-hackers(at)postgresql(dot)org, pgsql-general(at)postgresql(dot)org
Subject: Re: [HACKERS] random() function produces wrong range
Date: 2000-08-02 20:36:20
Message-ID: 1254.965248580@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-generalpgsql-hackers
Malcolm Beattie <mbeattie(at)sable(dot)ox(dot)ac(dot)uk> writes:
> Tom Lane writes:
>> Thomas Lockhart <lockhart(at)alumni(dot)caltech(dot)edu> writes:
>>>> The Linux man pages indicate that the behavior and underlying
>>>> implementation of random() and rand() are the same (so I just picked
>>>> one).
>> 
>> Ah, well, there's your problem.  Whoever did this part of the library
>> on Linux took shortcuts.

> Why? Linux is compliant with the Single Unix Standard v2 from my brief
> reading of it.

Sorry, bad choice of words on my part.  Linux is within its rights to
use the same underlying implementation for rand() and random(), but it
is a poor guide to the behavior of other systems, which are equally
within their rights not to.

>> none of the man pages I've looked at so far mention it).  But all the
>> machines say that the output of random() is 31 bits, so INT_MAX should
>> work.

> SuSv2 says explicitly 2^31-1 so you should use that, otherwise you'll
> be non-portable to platforms with 64-bit ints, for example.

Maybe.  You don't think that a 64-bit-int platform would choose to
supply a random() function with a range of 2^63-1?  The HPUX and SunOS
man pages clearly specify that random()'s result is "long", so I think
a case could also be made for LONG_MAX.

I suspect we have a good chance at getting burned no matter what we use
:-(.  But RAND_MAX is definitely the wrong thing.

			regards, tom lane

In response to

Responses

pgsql-hackers by date

Next:From: Tatsuo IshiiDate: 2000-08-03 01:09:07
Subject: comparing rows
Previous:From: David Lloyd-JonesDate: 2000-08-02 16:33:01
Subject: Is There Anybody in Toronto Running Postgresql with AOLserver?

pgsql-general by date

Next:From: Christopher SmithDate: 2000-08-02 23:26:01
Subject: Re: Slash on PostgreSQL mailing list
Previous:From: Tom LaneDate: 2000-08-02 20:25:47
Subject: Re: disable multiple queries

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