According to the official documentation, RAND () has a value range of [0,1]
In MySQL, you can perform the following commands to view:
Mysql>?RandName:'RAND'Description:syntax:RAND(),RAND(N)ReturnsA random floating-Point value VinchTherange 0 <= v < 1.0.IfaconstantintegerArgument N isspecified, it isUsed asThe seed Value,which produces aRepeatableSequence of column Values.inchthe Followingexample, note that the sequences of ValuesProduced by RAND(3) isthesame both placeswhereit occurs. Url:http://Dev.mysql.com/Doc/Refman/5.6/En/Mathematical-Functions.htmlExamples:mysql> CREATE TABLET (iINT); Query OK,0Rows Affected (0.42sec) MySQL> INSERT intoTVALUES(1),(2),(3); Query OK,3Rows Affected (0.00sec) Records:3Duplicates:0Warnings:0MySQL> SELECTIRAND() fromT;+------+------------------+|I| RAND()|+------+------------------+| 1 | 0.61914388706828 || 2 | 0.93845168309142 || 3 | 0.83482678498591 |+------+------------------+3Rowsinch Set(0.00sec) MySQL> SELECTIRAND(3) fromT;+------+------------------+|I| RAND(3)|+------+------------------+| 1 | 0.90576975597606 || 2 | 0.37307905813035 || 3 | 0.14808605345719 |+------+------------------+3Rowsinch Set(0.00sec) MySQL> SELECTIRAND() fromT;+------+------------------+|I| RAND()|+------+------------------+| 1 | 0.35877890638893 || 2 | 0.28941420772058 || 3 | 0.37073435016976 |+------+------------------+3Rowsinch Set(0.00sec) MySQL> SELECTIRAND(3) fromT;+------+------------------+|I| RAND(3)|+------+------------------+| 1 | 0.90576975597606 || 2 | 0.37307905813035 || 3 | 0.14808605345719 |+------+------------------+3Rowsinch Set(0.01Sec
To get a random integer R in the I≤r≤j range, you need to use the expression floor (i + RAND () * (J–i + 1))
For example, to get a random integer within the range of 7 to 12, including 7 and 12, use the following statement:
SELECT Floor (7 + (RAND () * 6));
In addition, you can also use the round rounding function to achieve, taking into account that the first and last value probability will be unequal with the middle, so add 0.5来 to eliminate the difference in probability, to achieve a uniform distribution:
Generating [i,j] range positive integers:select round (rand () * (j-i+1) +i-0.5)
To get a random integer within the range of 7 to 12, including 7 and 12, use the following statement:
SELECT round (RAND () * 6+6.5));
If a negative integer is generated, it is possible to note that the leftmost value may exceed your requirements:
Mysql> SELECT round(-0.5);+-------------+| round(-0.5)|+-------------+| -1 |+-------------+1Rowinch Set(0.00sec) MySQL> SELECT round(-0.4);+-------------+| round(-0.4)|+-------------+| 0 |+-------------+1Rowinch Set(0.00Sec
MySQL rand () generates a random integer range and method