Let's say I'm creating a table foo with a column bar that should be a very large random integer.
CREATE TABLE foo (
bar bigint DEFAULT round(((9223372036854775807::bigint)::double precision * random())) NOT NULL,
baz text
);
Is this the best way to do this? Can anyone speak to the quality of PostgreSQL's random()
function? Is the multiplication here masking the entropy?
Note that I do have good hardware entropy feeding into /dev/random
.
The random function will return a completely random number if no seed is provided (seed is set with the setseed function). The random function will return a repeatable sequence of random numbers each time a particular seed value is used (seed is set with the setseed function).
random() function generates random floating numbers in the range[0.1, 1.0). (See the opening and closing brackets, it means including 0 but excluding 1). It takes no parameters and returns values uniformly distributed between 0 and 1.
The random() function of postgreSQL is used to return a random number between 0 and 1 . This function can be used to generate a random number within a range.
The PostgreSQL Provides a random() function to generate a random string with all the possible different numbers, character and symbol. We can also use random() function with cryptography or encryption function to generate a fixed length binary string.
Postgresql random is based on their own portable implementation of POSIX erand48. It's a linear congruential PRNG in a 48 bit domain.
If you need something stronger look to the pg_crypto module's gen_random_bytes function which is used to produce cryptographically strong entropy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With