The
RAND() function in MySQL is used to a return random floating-point value
V in the range
0 <= V < 1.0. If we want to obtain a random integer R in the range
i <= R < j, we have to use the expression :
FLOOR(i + RAND() * (j − i)).
Syntax :
RAND(N)
Parameter : This method accepts only one parameter.
N : If N is specified, it returns a repeatable sequence of random numbers. If no N is specified, it returns a completely random number. It is optional and it works as a seed value.
Returns : It returns a random floating number between 0 and 1.
Example-1 :
Obtaining a random value between 0 and 1 using RAND Function.
SELECT RAND() AS Random_Number;
Output :
Random_Number |
0.6332025068189973 |
Example-2 :
Obtaining random value between 0 and 1 using RAND Function with seed value.
SELECT RAND(), RAND(5), RAND(5);
Output :
RAND() |
RAND(5) |
RAND(5) |
0.9580191140603452 |
0.40613597483014313 |
0.40613597483014313 |
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.
Example-3 :
Obtaining random value between in the range
[ 5, 10 ) using RAND Function. Here, we will use the expression :
FLOOR(i + RAND() * (j − i)) for generating the random number. Here, i will be 5 and j will be 10 .
SELECT FLOOR(5 + RAND()*(10-5)) AS Random_Number;
Output :
Example-4 :
Obtaining random value between in the range
[ 5, 10 ] using RAND Function. Here, we will use the expression :
FLOOR(i + RAND() * (j − i + 1)) for generating the random number. Here
i will be 5 and
j will be 10.
SELECT FLOOR(5 + RAND()*(10 - 5 + 1)) AS Random_Number;
Output :
Example-5 :
Using RAND Function to return rows from a category table by random order. To demonstrate create a table named
Student.
CREATE TABLE Student(
Student_id INT AUTO_INCREMENT,
Student_name VARCHAR(100) NOT NULL,
Student_Class VARCHAR(20) NOT NULL,
TotalExamGiven INT NOT NULL,
PRIMARY KEY(Student_id )
);
Now inserting some data to the Student table -
INSERT INTO
Student(Student_name, Student_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 Student Table we will use -
SELECT *
FROM Student;
Output :
Student_id |
Student_name |
Student_Class |
TotalExamGiven |
1 |
Sayan |
IX |
8 |
2 |
Nitin |
X |
5 |
3 |
Aniket |
XI |
6 |
4 |
Abdur |
X |
7 |
5 |
Riya |
IX |
4 |
6 |
Jony |
X |
10 |
7 |
Deepak |
X |
7 |
8 |
Ankana |
XII |
5 |
9 |
Shreya |
X |
8 |
So, we can see that all rows in the table are given in the right order. To return rows from the Student table by a random order we will use -
SELECT *
FROM Student
ORDER BY RAND();
Output :
Student_id |
Student_name |
Student_Class |
TotalExamGiven |
6 |
Jony |
X |
10 |
1 |
Sayan |
IX |
8 |
5 |
Riya |
IX |
4 |
2 |
Nitin |
X |
5 |
3 |
Aniket |
XI |
6 |
8 |
Ankana |
XII |
5 |
9 |
Shreya |
X |
8 |
4 |
Abdur |
X |
7 |
7 |
Deepak |
X |
7 |
Similar Reads
RPAD() Function in MySQL
RPAD() function in MySQL is used to pad or add a string to the right side of the original string. Syntax : RPAD(str, len, padstr) Parameter : This function accepts three parameter as mentioned above and described below : str : The actual string which is to be padded. If the length of the original st
1 min read
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
ORD() Function in MySQL
ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from
3 min read
ROUND() Function in MySQL
The ROUND() function in MySQL is used to round a number to a specified number of decimal places. If no specified number of decimal places is provided for round-off, it rounds off the number to the nearest integer. Syntax ROUND(X, D) Parameter Explanation This method accepts two parameters in the syn
2 min read
RADIANS() Function in MySQL
RADIANS() function in MySQL is used to convert the degree values into radians. The formula for converting degree to radian is : 180 degrees = Ï radian Syntax : RADIANS(X) Parameter : This method accepts only one parameter. X : The degree value which we convert to radian. Returns : It returns equival
2 min read
STD() function in MySQL
With the help of STD() function we can calculate population Standard deviation of an expression in MySQL. But, if there are no matching rows in the given expression it returns Null. Syntax : STD(expr); Parameter : This method accepts only one parameter. expr : Input expression from which we want to
3 min read
VARIANCE() function in MySQL
Sometimes we need to calculate population Standard variance of an expression in MySQL. VARIANCE() function can be used for this purpose in MySQL. It returns NULL if in the given expression no matching rows are found. Syntax : VARIANCE(expr); Parameter : This method accepts only one parameter. expr:
3 min read
SECOND() Function in MySQL
SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu
2 min read
SQRT() Function in MySQL
The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 min read
PI() function in MySQL
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read