From: Tom Lane <tgl@sss.pgh.pa.us>
To: Phillip Diffley <phillip6402@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: How to generate random bigint
Date: Thu, 21 Dec 2023 01:20:54 -0500
Message-ID: <594221.1703139654@sss.pgh.pa.us> (raw)
In-Reply-To: <CAGAwPgQt6B0ORW_YfwsdRmWPhb_pG6c9HM7S=G4WGC_s8qaU+w@mail.gmail.com>
References: <CAGAwPgQt6B0ORW_YfwsdRmWPhb_pG6c9HM7S=G4WGC_s8qaU+w@mail.gmail.com>
Phillip Diffley <phillip6402@gmail.com> writes:
> Postgres's random() function generates a random double. That can be
> converted to a random int for smaller integers, but a double can't
> represent all of the values in a bigint. Is there a recommended way to
> generate a random bigint in Postgres?
Doesn't look like there's anything directly exposed for that.
Since PG v13 you could use gen_random_uuid, if you're careful
to extract only bits that aren't fixed by the v4 UUID spec.
pgcrypto's pg_random_bytes() function offers another
some-assembly-required solution that'd work considerably
further back. Or you could make a custom C function that
leverages pg_strong_random().
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, phillip6402@gmail.com
Subject: Re: How to generate random bigint
In-Reply-To: <594221.1703139654@sss.pgh.pa.us>
References: <CAGAwPgQt6B0ORW_YfwsdRmWPhb_pG6c9HM7S=G4WGC_s8qaU+w@mail.gmail.com> <594221.1703139654@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox