MySQL Tutorial/Math Numeric Functions/RAND

Материал из SQL эксперт
Версия от 09:49, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

RAND(20)

SELECT RAND(20);
+------------------+
| RAND(20)         |
+------------------+
| 0.15888261251047 |
+------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT RAND(20);
+------------------+
| RAND(20)         |
+------------------+
| 0.15888261251047 |
+------------------+
1 row in set (0.00 sec)
mysql>


RAND(), RAND(N) returns a random floating-point value v in the range 0

If a constant integer argument N is specified, it is used as the seed value, which produces a repeatable sequence of column values.



mysql>
mysql> SELECT RAND();
+------------------+
| RAND()           |
+------------------+
| 0.72570215698863 |
+------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT RAND(20);
+------------------+
| RAND(20)         |
+------------------+
| 0.15888261251047 |
+------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT RAND(20);
+------------------+
| RAND(20)         |
+------------------+
| 0.15888261251047 |
+------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT RAND();
+------------------+
| RAND()           |
+------------------+
| 0.49934750655605 |
+------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT RAND();
+------------------+
| RAND()           |
+------------------+
| 0.31963109254803 |
+------------------+
1 row in set (0.00 sec)


To obtain a random integer in the range the range 7

mysql>
mysql> SELECT FLOOR(7 + (RAND() * 5));
+-------------------------+
| FLOOR(7 + (RAND() * 5)) |
+-------------------------+
|                       7 |
+-------------------------+
1 row in set (0.00 sec)
mysql>