MySQL RAND Function

MySQL RAND function is one of the Mathematical Functions used to return or generate the random numbers between 0 and 1. The syntax of the RAND is as shown below:

SELECT RAND();

SELECT RAND(n);

Generally, this MySQL RAND function generates numbers between 0.0 to 1.0. However, if you want a random integer, then you have to follow the below technique.

SELECT FLOOR(10 + RAND() * 30);

This generates the integer number between 10 and 30.

MySQL RAND Function Example

This RAND Function returns the random values, and the following query shows multiple ways to use this one.

SELECT RAND();

SELECT RAND();

SELECT RAND(10), RAND(120), RAND(-150);
MySQL RAND Function Example 1

It also allows you to return or store random values inside a column. In this Mathematical method example, we are generating numbers for each employee in a customer table. Please refer to In this Mathematical method in MySQL.

SELECT EmpID, 
       FirstName,
       LastName,
       Occupation,
       RAND(),
       RAND(5),
       RAND(10)
 FROM customer;
MySQL RAND Function Example 2