×

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 NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL MOD() function

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

3 minutes read.

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL RADIANS() function

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

2 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL DATE_ADD() function

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

3 minutes read.

MySQL Character Length Function

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

2 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 Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL NOW() function

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

2 minutes read.

MySQL FROM_BASE64() function

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

2 minutes read.

MySQL ROUND() function

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

3 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.