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
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
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
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
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
REPLACE() Function in MySQL The REPLACE() function in MySQL is a powerful tool for string manipulation, allowing users to substitute specific substrings within a larger string. This functionality is particularly useful in various applications such as updating text data, cleaning up input or adjusting content in a database. In
3 min read
RIGHT() Function in MySQL RIGHT() function in MySQL is used to extract a specified number of characters from the right side of a given string. Second argument is used to decide, how many characters it should return. Syntax : RIGHT( str, len ) Parameter : This function accepts two parameter as mentioned above and described be
3 min read
SUM() Function in MySQL The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
SPACE() Function in MySQL SPACE() function in MySQL is used to return a string consisting of specified empty space characters. Syntax : SPACE(num) Parameter : This method accepts one parameter as mentioned above and described below : num : It is an integer which indicates how many spaces are being contained. Returns : It ret
1 min read