PostgreSQL does not use extensions, generates random int8 values, generates uniformly distributed random int8 values

PostgreSQL uses the extension plug-in pgcrypto to generate random int8 values

In PostgreSQL, you can use pgcryptothe function provided by the extension gen_random_uuid()to generate a random UUID (Universally Unique Identifier), which contains an 8-byte random integer (int8 value), as follows:

CREATE EXTENSION IF NOT EXISTS "pgcrypto";

SELECT substring(gen_random_uuid()::text from 1 for 16)::bit(64)::bigint;

The above SQL statement uses gen_random_uuid()the function to generate a random UUID and convert it to a string type. Then, use substring()the function to extract the first 16 characters from the string and convert them to bit(64)type. Finally, use bigintthe type to convert the value to get a random 8-byte integer (int8 value).

PostgreSQL does not use extensions, generates random int8 values

Instead of using pgcryptothe extension, you can use the Postgres built-in random()function to generate random numbers and convert them to the integer type you need. The following is an example SQL statement that generates a random 8-byte integer:

SELECT floor(random() * 9223372036854775807)::bigint;

In the above statement, random()the function returns a floating point number between [0, 1), multiply it by 9223372036854775807 (2^63 - 1), and get a floating point number between [0, 9223372036854775807). Then use floor()the function to convert it to an integer, and use to ::bigintconvert it to a 64-bit integer type (bigint).

Note that since random()the function returns floating point numbers, the integers generated are not evenly distributed. If you need better random performance, consider using pgcryptothe random number generation functions provided in the extension.

PostgreSQL does not use extensions, generates uniformly distributed random int8 values

If you need to generate uniformly distributed random int8 values, you can use the Postgres built-in random()function and bitwise operators to generate them. The following is an example SQL statement that generates a uniformly distributed random 8-byte integer:

SELECT trunc(random()*pow(2,64)::numeric)::bigint^trunc(random()*pow(2,64)::numeric)::bigint;

In the above statement, we use pow()the function to calculate the maximum value (2 to the 64th power) of a 64-bit unsigned integer, and then convert the value to numericthe type. Next, we use random()the function twice to generate two random numbers between 0 and 1 and multiply them by the maximum value, resulting in two potential 64-bit unsigned integer values. Convert them to integers using trunc()the function and bitwise XOR them individually to get a uniformly distributed random 8-byte integer.

It's worth noting that this approach isn't a perfect uniform distribution either, but it's good enough for most cases. If you need higher quality random integers, use pgcryptothe random number generation functions provided in the extension.

Guess you like

Origin blog.csdn.net/sunny_day_day/article/details/131137386