The Postgres random() function generates random floating point values between 0.0 and 1.0. Starting with Postgres 17, it also supports generating random integers or decimals within a specified range using random(min, max) syntax.
It's particularly useful for creating some sample data, usage in simulations, or introducing randomness in queries for applications like statistical sampling and testing algorithms.
Function signatures
The random() function has the following signatures:
The first form returns a uniformly distributed random value between 0.0 (inclusive) and 1.0 (exclusive).
Starting from Postgres 17, the function also accepts range parameters:
For integer types, it returns a random integer between min and max (inclusive)
For numeric types, it returns a random decimal number between min and max (inclusive). The result will have the same number of decimal places as the input parameter with the highest precision.
Example usage
Basic random number generation
Let's create a table of simulated sensor readings with random values:
The generate_series() function is used to generate a series of integers from 1 to 5, which is then used to create the sensor names. Then, random() is used to generate random temperature and humidity values within specific ranges.
Random integer within a range
Let's simulate a dice game where each player rolls two dice, and we calculate the total:
This simulates 5 players each rolling two dice, with random values between 1 and 6 for each die. Notice how we can now use the simpler random(1, 6) syntax instead of the more complex 1 + floor(random() * 6)::INTEGER typically used in earlier versions of Postgres.
Other examples
Using random() for sampling
Suppose we have a large table of customer data and want to select a random sample for a survey:
This query selects approximately 1% of the customers randomly by filtering for rows where random() is less than 0.01.
Combining random() with other functions
You can use random() in combination with other functions to generate more complex random data. For example, let's create a table of random events with timestamps within the last 24 hours:
This creates 100 random events with different types, severities, and timestamps within the last 24 hours.
Additional considerations
Seed for reproducibility
The Postgres random() function uses a seed that is initialized at the start of each database session. If you need reproducible random numbers across sessions, you can set the seed manually using the setseed() function:
This will produce the same sequence of random numbers in any session where you set the same seed. The setseed() function takes a value between 0 and 1 as its argument.
Performance implications
The random() function is generally fast, but excessive use in large datasets or complex queries can impact performance. For high-performance requirements, consider generating random values in application code or using materialized views with pre-generated random data.
Alternative functions
gen_random_uuid(): Generates a random UUID, useful when you need unique identifiers.