How to Generate a Random Number in a Range
Summary: This tutorial shows you how to develop a user-defined function that generates a random number between two numbers.
PostgreSQL provides the random()
function that returns a random number between 0 and 1. The following statement returns a random number between 0 and 1.
To generate a random number between 1 and 11, you use the following statement:
If you want to generate the random number as an integer, you apply the floor()
function to the expression as follows:
Generally, to generate a random number between two integers low and high, you use the following statement:
You can develop a user-defined function that returns a random number between two numbers low
and high
:
The following statement calls the random_between()
function and returns a random number between 1 and 100:
If you want to get multiple random numbers between two integers, you use the following statement:
In this tutorial, you have learned how to generate a random number between a range of two numbers.