EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL RANDOM
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

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

Syntax

Below is the syntax of a random function in PostgreSQL.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

PostgreSQL RANDOM 1

Code:

select version();

Output:

PostgreSQL RANDOM 2

  • 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:

PostgreSQL RANDOM 3

Code:

select random();

Output:

PostgreSQL RANDOM 5

  • 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:

PostgreSQL RANDOM 6

Code:

select random();

Output:

PostgreSQL RANDOM 7

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:

PostgreSQL RANDOM 8

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:

PostgreSQL RANDOM 10

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:

PostgreSQL RANDOM 11

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 –

  1. PostgreSQL datediff
  2. PostgreSQL Date Functions
  3. PostgreSQL Drop Schema
  4. PostgreSQL Datetime
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
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

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