EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL RANDOM

PostgreSQL RANDOM

By Priya PedamkarPriya Pedamkar

PostgreSQL RANDOM

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 a random function to generate the random number of two integers in PostgreSQL.

Syntax

Below is the syntax of a random function.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Select random();

(Select the default random value by using a random function)

Select random () * 20+10;

(We can use the default number of random functions by using a mathematical operator)

Select floor(random () * 10+5) :: int ;

(We have used the floor function in the 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 and 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 the 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 is 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:

PostgreSQL RANDOM 1

Code:

select version();

Output:

PostgreSQL RANDOM 2

  • In the above example, when we select a random number, the first time value of the random number is 0.11. The second time it will be 0.049; it will state that the default random value will change 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 a 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 functions 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 a set seed in PostgreSQL random number are as follows.

Code:

SELECT setseed(0.47);

Output:

set seed

Code:

select random();

Output:

set seed

  • In the above example, after we use the set seed function and pass the 0.47 value, it will show the same random value every time.
  • We need to run the same set seed value every time to get the same value at every time in PostgreSQL.

Examples

Below is the example mentioned:

Example #1

The below example shows the random number in PostgreSQL. In the example below, we select any number from the default value in PostgreSQL.

Code:

select random();

Output:

PostgreSQL RANDOM 6

Code:

select random();

Output:

PostgreSQL RANDOM 7

Explanation: Select any default random number using PostgreSQL’s random function. In the above example, when we select a random number, the first time value of the random number is 0.32. The second time it will be 0.92; it will state that the default random value will change every time.

Example #2

In the example below, we use the random function by using integer numbers like 15 and 10. In the example below, we have to multiply any random value by adding 15 and 10.

Code:

Select random () * 15+10 as Random_number;

Output:

PostgreSQL RANDOM 8

Explanation: Example of a random function in PostgreSQL using integer values. The above example shows that we have to multiply random numbers from the addition of 15 and 10.

Example #3

We are using the floor function by using the random function. The example below shows a floor function using a random function in PostgreSQL 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:

floor function

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 an example of a user-defined random function 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:

user-defined random function

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.
  • In PostgreSQL, we can repeatedly choose the same integer using the set seed function.
  • We can choose any number of a 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

We hope that this EDUCBA information on “PostgreSQL RANDOM” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL datediff
  2. Date Functions PostgreSQL
  3. PostgreSQL Drop Schema
  4. PostgreSQL Datetime
PROGRAMMING LANGUAGES Course
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Certification Course
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System - Design & Develop an IOT System
65+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Certification Course
19+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
POSTGRESQL Certification Course
 17+ Hour of HD Videos
4 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more