ExampleReturn a random decimal number (no seed value - so it returns a completely random number >= 0 and <1): Show SELECT RAND(); Try it Yourself » Definition and UsageThe RAND() function returns a random number between 0 (inclusive) and 1 (exclusive). SyntaxRAND(seed) Parameter Values
Technical Details
More ExamplesExampleReturn a random decimal number (with seed value of 6): SELECT RAND(6); Try it Yourself » ExampleReturn a random decimal number >= 5 and <10: SELECT RAND()*(10-5)+5; Try it Yourself » ExampleReturn a random number >= 5 and <=10: SELECT FLOOR(RAND()*(10-5+1)+5); Try it Yourself » RAND() functionMySQL RAND() returns a random floating-point value between the range 0 to 1. When a fixed integer value is passed as an argument, the value is treated as a seed value and as a result, a repeatable sequence of column values will be returned. Syntax: RAND(), RAND(M); Argument:
Pictorial presentation of MySQL RAND() function Example of MySQL RAND() function Code:
Explanation: The above MySQL statement will return a random value between 0 and 1. Sample Output: mysql> SELECT RAND(); +-------------------+ | RAND() | +-------------------+ | 0.369500624360052 | +-------------------+ 1 row in set (0.00 sec) Example: RAND() function with seed value Code:
Explanation: The above MySQL statement will return a random value (between 0 and 1) and the repeatable value using seed in the argument. Sample Output: mysql> SELECT RAND(),RAND(2),RAND(2); +-------------------+-------------------+-------------------+ | RAND() | RAND(2) | RAND(2) | +-------------------+-------------------+-------------------+ | 0.964232316207357 | 0.655586646549019 | 0.655586646549019 | +-------------------+-------------------+-------------------+ 1 row in set (0.00 sec) Example: RAND() function using table Code:
Explanation: The above MySQL statement will return the largest integer value after multiply the randomly generated number by 10 but not greater than the generated number. Sample Output: mysql> SELECT FLOOR(RAND()*10) -> FROM category; +------------------+ | FLOOR(RAND()*10) | +------------------+ | 7 | | 6 | | 2 | | 0 | | 6 | +------------------+ 5 rows in set (0.29 sec) Example: RAND() function with where clause Code:
Explanation: The above MySQL statement will return those rows from publisher table which have no_of_branchs are more than the greatest number after generating the random number with FLOOR function. Sample Output: mysql> SELECT pub_name,country,no_of_branch, -> FLOOR(RAND(2)*20) -> FROM publisher -> WHERE no_of_branch>FLOOR(RAND(2)*20); +--------------------------+---------+--------------+-------------------+ | pub_name | country | no_of_branch | FLOOR(RAND(2)*20) | +--------------------------+---------+--------------+-------------------+ | Jex Max Publication | USA | 15 | 13 | | BPP Publication | India | 10 | 2 | | Mountain Publication | USA | 25 | 12 | | Summer Night Publication | USA | 10 | 17 | | Novel Publisher Ltd. | India | 10 | 7 | +--------------------------+---------+--------------+-------------------+ 5 rows in set (0.13 sec) PHP script:
View the example in browser Example : RAND() function using order by Code:
Explanation: The above MySQL statement will return rows from category table by a random order. Sample Output: mysql> SELECT * FROM category -> ORDER BY RAND(); +---------+--------------+ | cate_id | cate_descrip | +---------+--------------+ | CA001 | Science | | CA005 | Medical | | CA003 | Computers | | CA004 | Nature | | CA002 | Technology | +---------+--------------+ 5 rows in set (0.05 sec) PHP script:/strong>
View the example in browser Online Practice Editor: All Mathematical Functions Previous: RADIANS() What does rand () function do?Description. RAND returns an evenly distributed random real number greater than or equal to 0 and less than 1. A new random real number is returned every time the worksheet is calculated.
What is Rand in SQL?SQL Server RAND() Function
The RAND() function returns a random number between 0 (inclusive) and 1 (exclusive).
Is rand () defined in library?rand() is defined in the standard library. It is declared in the headers <stdlib.
What is difference between rand () and Srand ()?The srand() function sets the starting point for producing a series of pseudo-random integers. If srand() is not called, the rand() seed is set as if srand(1) were called at the program start. Any other value for seed sets the generator to a different starting point.
|