The Postgres random() function generates random floating point values between 0.0 and 1.0.
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 signature
The random() function has a simple form:
It returns a uniformly distributed random value between 0.0 (inclusive) and 1.0 (exclusive).
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
To generate random integers within a specific range, we can use the random() function in combination with other operations. Here's an example simulating a dice rolling game where players roll two six-sided dice:
This simulates 5 players each rolling two dice, with random values between 1 and 6 for each die.
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.