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
REPEAT() function in MySQL
REPEAT() : This function in MySQL is used to repeat a string a specified number of times. Syntax : REPEAT(str, count) Parameters : This method accepts two parameter. str -Input String which we want to repeat. count -It will describe that how many times to repeat the string. Returns : It returns a re
2 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
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
SIGN() Function in MySQL
SIGN() function in MySQL is used to return the sign of the given number. It returns 1 if the number is positive, -1 if the number is negative and 0 for zero. Syntax : SIGN(X) Parameter : SIGN() function accepts one parameter as input and will give you the results in values like Positive(+1),Negative
1 min read
TRIM() Function in MySQL
TRIM() function in MySQL is used to clean up data. It is also used to remove the unwanted leading and trailing characters in a string. Syntax : TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str) Parameter : This method accepts three-parameter as mentioned above and described below : BOTH | LEADIN
2 min read
UUID() function in MySQL
In MySQL, the UUID() function is used to generate a Universal Unique Identifier (UUID), which is a 128-bit value that is globally unique. This function adheres to the RFC 4122 specification for creating universally unique identifiers. The generated UUID is especially useful for distributed systems,
6 min read
POW() Function in MySQL
POW() function : This function in MySQL is used to return a results after raising a specified exponent number to a specified base number. For example if the base is 5 and exponent is 2, this will return a result of 25. Syntax : SELECT POW(x, y); Parameter : This method accepts two parameters as give
1 min read