Re: How to generate random bigint

2023-12-22 Thread Junwang Zhao
On Sat, Dec 23, 2023 at 8:36 AM Phillip Diffley wrote: > > Thank you for all the suggestions! I ended up using pgcrypto's > pg_random_bytes() to build the random int. I haven't fully tested the > function yet, but it looks like this works. > > CREATE EXTENSION IF NOT EXISTS pgcrypto; > CREATE OR

Re: How to generate random bigint

2023-12-22 Thread Phillip Diffley
Thank you for all the suggestions! I ended up using pgcrypto's pg_random_bytes() to build the random int. I haven't fully tested the function yet, but it looks like this works. CREATE EXTENSION IF NOT EXISTS pgcrypto; CREATE OR REPLACE FUNCTION gen_random_int() RETURNS INT8 AS $$ DECLARE bytes

Re: How to generate random bigint

2023-12-21 Thread Peter J. Holzer
On 2023-12-21 00:06:39 -0600, Phillip Diffley wrote: > 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 >

Re: How to generate random bigint

2023-12-20 Thread Thomas Munro
On Thu, Dec 21, 2023 at 7:21 PM Tom Lane wrote: > Phillip Diffley 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 > >

Re: How to generate random bigint

2023-12-20 Thread Tom Lane
Phillip Diffley 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