Introduction to PostgreSQL RANDOM
PostgreSQL random function is mostly useful to return a random value between 0 and 1; the default result of a random result is different at every time of the query execution. We can also return the random number between the specified range and values. The random function is essential and useful in PostgreSQL to select any random number between a series of values. If we want to generate a random number in an integer value, we need to use a floor function in random function to generate the random number of two integers in PostgreSQL.
Syntax
Below is the syntax of a random function in PostgreSQL.
Select random();
(Select default random value by using a random function)
Select random () * 20+10;
(We can use the default number of random function by using a mathematical operator)
Select floor(random () * 10+5) :: int ;
(We have used floor function in random function)
Parameter
Below is the parameter description of the above syntax.
- Select: Select is used to select any random value using the random function in PostgreSQL. We can also select a random value using any integer value; also, we can select two series of values using a random function.
- Random (): Random function is essential and useful in PostgreSQL to select any random number between a series of values. This function is mostly useful to return a random value between 0 and 1; the default result of random value is different at every time of the query execution.
- Floor (): If we want to generate a random number in an integer value, then we need to use a floor function in the random function.
- Integer value: We can use any integer value to generate a random number in PostgreSQL. We can use the mathematical operator in an integer value.
How does PostgreSQL RANDOM Function work?
Below is the working:
- The random function in PostgreSQL used to select any random number or values from selected integer values.
- We can select a random value using the default function. But at the time of selecting the default value, it will look different every time.
The below example shows that every time the default value of the random function is different.
Code:
select random();
Output:
Code:
select version();
Output:
- In the above example, when we select a random number first time value of the random number is 0.11. The second time it will be 0.049; it will state default random value will change at every time.
- This function is mostly useful to return a random value between 0 and 1; the default result of a random result is different at every time of the query execution.
- The random function is essential and useful in PostgreSQL to select any random number between a series of values.
- We can also return a random number between the specified range and values.
- If we want to generate a random number in an integer value, we need to use a floor function in random function to generate the random number of two integers in PostgreSQL.
- This function is used to select a random number from any integer values.
- We can also generate user-defined random function using create function. We can create a user-defined function by using the random function in PostgreSQL.
- If we set the same random data every time, then we need to define the function as a set seed. Inset seed, we need to pass the argument as an integer value.
Below is the example of set seed in PostgreSQL random number are as follows.
Code:
SELECT setseed(0.47);
Output:
Code:
select random();
Output:
- In the above example, after we have using the set seed function and passing the 0.47 value, it will show the random value the same at every time.
- We need to run the same set seed value every time to get value the same at every time in PostgreSQL.
Example to Implement RANDOM Function in PostgreSQL
Below is the example mentioned:
Example #1
The below example show the random number in PostgreSQL. In the below example, we have select any number from the default value in PostgreSQL.
Code:
select random();
Output:
Code:
select random();
Output:
Explanation: Select any default random number by using the random function in PostgreSQL. In the above example, when we select a random number first time value of the random number is 0.32. The second time it will be 0.92; it will state default random value will change at every time.
Example #2
In the below example, we have using random function by using integer numbers like 15 and 10. In the below example, we have to multiply any random value from the addition of 15 and 10.
Code:
Select random () * 15+10 as Random_number;
Output:
Explanation: Example of a random function in PostgreSQL by using integer values. The above example shows that we have to multiply random numbers from the addition of 15 and 10.
Example #3
Using floor function by using random function. The below example shows a floor function by using a random function in PostgreSQL are as follows. We have to use the floor function by using multiply random numbers from the addition of 20 and 30.
Code:
Select floor (random () * 20+30) :: int;
Output:
Explanation: Example of a random function in PostgreSQL by using the floor function.
Example #4
Create a user-defined function by using the user-defined function. Below is the example of a user-defined random function are as follows.
Code:
CREATE OR REPLACE FUNCTION Random_Test (Min INT ,MAX INT) RETURNS INT AS
$$
BEGIN
RETURN floor(random()* (MAX-Min + 1) + Min);
END;
$$ language 'plpgsql' STRICT;
SELECT Random_Test(100,1000);
SELECT Random_Test(100,1000);
Output:
Explanation: Example of a user-defined function by using the function.
Advantages of using RANDOM Functions in PostgreSQL
Below are the advantages:
- We can generate any number by using the function in PostgreSQL.
- We can use the set seed function to select the same number every time in PostgreSQL.
- We can choose any number of the large number of groups using the function in PostgreSQL.
- It will choose a simple number from a large number of the group by using the function in PostgreSQL.
- This user-defined function is created by using a random function in PostgreSQL.
Conclusion
We can generate user-defined functions using create the function in PostgreSQL. This function is mostly useful to return a random value between 0 and 1; the default result of a random result is different at every time of the query execution. The random number is beneficial in PostgreSQL.
Recommended Articles
This is a guide to PostgreSQL RANDOM. Here we discuss the introduction to PostgreSQL RANDOM along with appropriate syntax and respective examples. You can also go through our other related articles to learn more –