PostgreSQL - Random Number Generation
Last Updated :
16 Aug, 2024
When working with databases, there are times when you might need to generate random numbers for various use cases such as sampling data, testing, or creating unique values. PostgreSQL offers the built-in random() function, which returns a random number between 0 and 1. However, you can go beyond that by generating random numbers within a specific range or even creating a user-defined function (UDF) for more flexibility. This article will look into how to achieve this and provide practical examples.
PostgreSQL random() Function
The random() function in PostgreSQL generates a random floating-point number between 0 (inclusive) and 1 (exclusive). This can be particularly useful in a variety of scenarios.
Syntax:
Here's the basic syntax:
SELECT random();
If you try the above syntax it will lead to the following:

Generating Random Numbers Within a Range
To generate a random number between 1 and 10, you use the following statement:
SELECT random() * 10 + 1 AS RAND_1_10;
If you try the above syntax it will lead to the following:

If you want to generate the random number as an integer, you apply theĀ floor() function to the expression as follows:
SELECT floor(random() * 10 + 1)::int;
The above query results in the following:

The result will be an integer between 1 and 10.
Generating Random Numbers Between Two Custom Integers
Generally, to generate a random number between two integers l and h, you use the following statement:
SELECT floor(random() * (h-l+1) + l)::int;
For example, to generate a random number between 20 and 50:
SELECT floor(random() * (50 - 20 + 1) + 20)::int;
Creating a User-Defined Function for Random Numbers
To create a user-generated function that returns a random number between two numbers l and h:
CREATE OR REPLACE FUNCTION random_between(low INT ,high INT)
RETURNS INT AS
$$
BEGIN
RETURN floor(random()* (high-low + 1) + low);
END;
$$ LANGUAGE 'plpgsql' STRICT;
The following statement calls the random_between() function and returns a random number between 1 and 100:
SELECT random_between(1,100);
Output:

The output will be an integer between 1 and 100.
Generating Multiple Random Numbers
If you want to get multiple random numbers between two integers, you use the following statement.
SELECT random_between(1,100)
FROM generate_series(1,5);
Output:

This statement returns five random integers within the specified range.
Similar Reads
How to Select Random Row in PostgreSQL? Selecting random rows from a table in PostgreSQL can be a valuable feature for various applications, including data analysis, content generation, and gaming scenarios. PostgreSQL offers straightforward methods to achieve this, primarily through the RANDOM() function and the ORDER BY RANDOM() clause.
4 min read
PostgreSQL - CREATE SEQUENCE In database management, generating unique identifiers is vital for data integrity, and PostgreSQL provides a powerful feature called CREATE SEQUENCE to solve this. This command allows developers to create a sequence that automatically generates unique numeric values. In this article, we will explore
4 min read
RAND() Function in MySQL The RAND() function in MySQL is used to a return random floating-point value V in the range 0 <= V < 1.0. If we want to obtain a random integer R in the range i <= R < j, we have to use the expression : FLOOR(i + RAND() * (j â i)). Syntax : RAND(N) Parameter : This method accepts only on
3 min read
SQL - SELECT RANDOM In SQL, the RANDOM() function is used to fetch random rows from a table. It is an extremely useful function in various applications, such as selecting random users, retrieving random questions from a pool, or even for random sampling in data analysis. In this article, we will explore how to use RAND
3 min read
SQL Server RAND() Function The RAND() function in SQL Server generates pseudo-random floating-point numbers within the range of 0 (inclusive) and 1 (exclusive). It is a function that can produce different random values each time it is called unless a specific seed is provided which results in a repeatable sequence of numbers.
3 min read