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
PostgreSQL - Generate Columns
When working with databases, there are scenarios where you need a columnâs value to be automatically computed based on other columns. In PostgreSQL, this can be achieved through generated columns. These special columns are calculated based on an expression using other columns in the table. The value
3 min read
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 Auto-increment Column using SERIAL
In PostgreSQL, the SERIAL data type is a convenient way to create auto-increment columns, commonly used for primary keys. This feature simplifies the process of generating unique identifiers automatically without requiring additional manual input. By using SERIAL, BIGSERIAL, or SMALLSERIAL, PostgreS
4 min read
PostgreSQL - Identity Column
In PostgreSQL, an identity column is a specialized column type that automatically generates unique values for each row, making it ideal for primary keys and other unique identifiers. Introduced in PostgreSQL 10, the GENERATED AS IDENTITY clause offers a SQL-standard alternative to the widely-used SE
4 min read
List the Last 5 Rows of a Result Set in PostgreSQL
PostgreSQL provides several methods to retrieve data from tables. One common approach involves using the SELECT statement with ORDER BY to sort data, with the option to use DESC for descending order. By understanding these basics, we can explore techniques to list the last few rows of a result set i
4 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
RAWTOHEX Function in PL/SQL
The objective is to use the function RAWTOHEX in PL/SQL code. Purpose : This inbuilt function is beneficial for the conversion of a raw value into a character value in its hexadecimal format. Syntax : RAWTOHEX(x) where x - raw value to be converted to hexadecimal value and hexadecimal equivalent in
2 min read
How to Request a Random Row in SQLite?
SQLite is a database engine that provides a relational database management system and is a C language library with features like self-contained, serverless, and high reliability. SQLite is different from other traditional SQL database engines like MySQL, Oracle, PostgreSQL, etc. Traditional database
4 min read
SQL Auto Increment
In SQL databases, a primary key is important for uniquely identifying records in a table. However, sometimes it is not practical to manually assign unique values for each record, especially when handling large datasets. To simplify this process, SQL databases offer an Auto Increment feature that aut
6 min read
How to Select Random Row in MySQL
In database operations, selecting random rows from a table is a common requirement for various applications, such as gaming, content recommendation, and statistical sampling. In this article, we learn different methods for selecting random rows in MySQL. We'll understand various approaches, includin
5 min read