Rand() function is used to select random numbers in Mysql. This function doesn’t get parameters. Returns a value between 0 and 1. To get a value between 0 and 100, we must multiply the value by 100 then round it up.
Example:
1 2 |
Select rand() --Returns 0.7996482707556787 |
Example
1 2 |
Select rand()*100 --Returns 79.96482707556787 |
Example:
1 2 3 |
SELECT CEILING(RAND()*100) --Returns 80 --Ceiling rounds up Example Ceiling(1.000001) = 2 |
Example
1 2 3 |
SELECT FLOOR(RAND()*100) --Returns 79 --Floor function rounds down Example Floor(1.9999999) = 1 |
So if we use Ceiling method never return 0 value. For random number between 1 and 100 on mysql, we must use the code below.
1 |
SELECT CEILING(RAND()*100) |
If we want to get value between 0 and 100 the we must use the code below.
1 |
SELECT FLOOR(RAND()*101) |