×

MySQL RAND() function

In this context, we will learn how we can use the MySQL RAND() function with proper syntax and good examples.

Introduction of MySQL RAND() function

Basically, in MySQL the RAND() function is used to return a random floating-point value V in the range 0 <= V < 1.0. We have to use the expression, when we want to derive a random integer R in the range using the following formula:

FLOOR(i + RAND() * (j - i)).

Syntax of the MySQL RAND() function

The syntax of the MySQL RAND() function is given as follows:

RAND(X)

Parameters or arguments used in MySQL RAND() function:

Only one parameter is accepted by the RAND() function in MySQL, which is given as follows:

X: If X is specified, it will return a repeatable sequence of random numbers. If no X is specified, it will return a completely random number. It is optional, and it will work as a seed value.

Returns:

It will return a random floating number between 0 and 1.

Application used for RAND() function:

The RAND() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL RAND() function:

Now we will look into some MySQL RAND() function examples and will explore how we can use the RAND function in MySQL.

Example-1 :

Here, we will derive the random value between 0 and 1 using RAND Function in MySQL.

SELECT RAND() AS Random_Number;

Output :

Random_Number
0.6332025068189973
1 row in set (0.00 sec)

Example-2 :

Here, we will derive the random value between 0 and 1 using RAND Function with seed value in MySQL.

SELECT RAND(), RAND(5), RAND(5);

Output:

RAND()RAND(5)RAND(5)
0.9580194543703452  0.40613253673014313  0.40613876543214313  

So, here we can see that if we use the same seed value for generating the random number, we will get the same random number as a result.

1 row in set (0.00 sec)

Example-3 :

Now we will derive the random value between the range [ 5, 10 ) using RAND Function in MySQL. Here, we will use the expression: FLOOR(i + RAND() * (j - i)) to generate the random number. Here, i will be 5, and j will be 10.

SELECT FLOOR(5 + RAND()*(10-5)) AS Random_Number;

Output:

Random_Number
6
1 row in set (0.00 sec)

Example-4 :

Now we will obtain the random value between the range [ 5, 10 ] using RAND Function in MySQL. Here, we will use the expression: FLOOR(i + RAND() * (j - i + 1)) to generate the random number. Here i will be 5, and j will be 10.

SELECT FLOOR(5 + RAND()*(10 - 5 + 1)) AS Random_Number;

Output:

Random_Number
10
1 row in set (0.00 sec)

Example-5 :

Now, from a category table by random order, we will use the RAND Function to return rows. To demonstrate, we have created a table named pupil.

CREATE TABLE Pupil(
Pupil_id INT AUTO_INCREMENT,
Pupil_name VARCHAR(100) NOT NULL,
Pupil_Class VARCHAR(20) NOT NULL,
TotalExamGiven INT NOT NULL,
PRIMARY KEY(Pupil_id )
);

Now add some data into the Pupil table –

INSERT INTO
Pupil(Pupil_name, Pupil_Class, TotalExamGiven)
VALUES
('Sayan,' 'IX,' 8),
('Nitin,' 'X,' 5),
('Aniket,' 'XI,' 6),
('Abdur,' 'X,' 7),
('Riya,' 'IX,' 4),
('Jony,' 'X,' 10),
('Deepak,' 'X,' 7),
('Ankana', 'XII', 5),
('Shreya,' 'X,' 8);

To get all details about Pupil Table, we will use the –

SELECT *
FROM Pupil;

Output:

Pupil_idPupil_namePupil_ClassTotalExamGiven
1SayanIX8
2NitinX5
3AniketXI6
4AbdurX7
5RiyaIX4
6JonyX10
7DeepakX7
8AnkanaXII5
9ShreyaX8

So, we can see that all rows in the table are given in the right order. To return rows from the Pupil table by a random order, we will use –

SELECT *
FROM Pupil
ORDER BY RAND();

Output:

Pupil_idPupil_namePupil_ClassTotalExamGiven
6JonyX10
1SayanIX8
5RiyaIX4
2NitinX5
3AniketXI6
8AnkanaXII5
9ShreyaX8
4AbdurX7
7DeepakX7

Summary:

In the above context, we have learned how to use MySQL's RAND() function to return a random floating-point value.


Related Topics

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.

MySQL PI() function

In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples. Introduction of MySQL PI() function PI() function in MySQL is used to...

2 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL CEILING() function

In this context, we will learn how we can use the MySQL CEILING () function with proper syntax and good examples. Introduction of MySQL CEILING () function The CEILING() function accepts the...

2 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL DAY() function

In this context, we will learn how we can use the MySQL DAY() function with proper syntax and good examples. Introduction of MySQL DAY() function This function in MySQL is used to...

2 minutes read.

MySQL INSTR() Function

In this context, we will learn how we can use the MySQL INSTR() function with proper syntax and good examples. Introduction of MySQL INSTR() function This function in MySQL returns the location...

4 minutes read.