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 in MySQL is as shown below:

SELECT RAND();

SELECT RAND(n);

Generally, this 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 random integer number between 10 and 30.

MySQL RAND Function Example 1

The MySQL Random Function returns the random values. The following query shows multiple ways to use the RAND function.

SELECT RAND();

SELECT RAND();

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

MySQL RAND Example 2

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

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