Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quality of PostgreSQL's random() function?

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.

like image 855
Dustin Kirkland Avatar asked Mar 22 '12 03:03

Dustin Kirkland


People also ask

How does Postgres random () work?

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).

What is the functionality of random?

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.

How do you generate unique random numbers in PostgreSQL?

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.

How do I generate random text in PostgreSQL?

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.


1 Answers

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.

like image 130
dbenhur Avatar answered Sep 17 '22 16:09

dbenhur