跳到主要内容

RAND

Returns a random floating-point value v in the range 0 <= v < 1.0. To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j − i)).

If an integer argument N is specified, it is used as the seed value. For equal argument values, RAND(N) returns the same value each time , and thus produces a repeatable sequence of column values (last example).

Syntax

RAND(N)

Arguments

ArgumentsDescription
NThe numerical value.

Return Type

A Float64 data type value.

Examples

SELECT RAND();
+---------------------+
| rand() |
+---------------------+
| 0.07669816779607708 |
+---------------------+

SELECT RAND(1), RAND(2);
+--------------------+--------------------+
| RAND(1) | RAND(2) |
+--------------------+--------------------+
| 0.9742447372584028 | 0.2973258311583632 |
+--------------------+--------------------+

SELECT number, RAND(1000 * number) from numbers(10);
+--------+-----------------------+
| number | RAND((1000 * number)) |
+--------+-----------------------+
| 0 | 0.7311134158637046 |
| 1 | 0.20926024019676037 |
| 2 | 0.3204522453024471 |
| 3 | 0.15414315241487253 |
| 4 | 0.3231494607676717 |
| 5 | 0.059500236508538085 |
| 6 | 0.7053594880492737 |
| 7 | 0.4848707366995819 |
| 8 | 0.2872295709183398 |
| 9 | 0.5819343845110294 |
+--------+-----------------------+

SELECT RAND(), RAND(0), RAND(10000), RAND(NULL) from numbers(10);
+---------------------+----------------------+---------------------+---------------------+
| RAND() | RAND(0) | RAND(10000) | RAND(NULL) |
+---------------------+----------------------+---------------------+---------------------+
| 0.8410192638206491 | 0.7311134158637046 | 0.9746737753675192 | 0.590583043081937 |
| 0.9356678073279926 | 0.7734601843532382 | 0.9819295977773548 | 0.19697177117599896 |
| 0.2746441697075338 | 0.025844634233355035 | 0.6053827707224846 | 0.6830698490445499 |
| 0.3092425785999753 | 0.5841592619362002 | 0.16167417938013184 | 0.7030024192421047 |
| 0.16685249404419444 | 0.26217063257252515 | 0.7859247691297169 | 0.2401736713222382 |
| 0.22077870630397123 | 0.7719869057468207 | 0.21038253398226503 | 0.9236027334502199 |
| 0.5202213774172745 | 0.2186969106715092 | 0.4705968522124563 | 0.9247213333056916 |
| 0.8547328275319827 | 0.7937134566691362 | 0.6019019906360323 | 0.33042828022969406 |
| 0.7626614422062121 | 0.7493384290992732 | 0.42479983417070444 | 0.48604290449390297 |
| 0.7180659634926269 | 0.9689377480023901 | 0.32660946286978787 | 0.7334682833042149 |
+---------------------+----------------------+---------------------+---------------------+

SELECT RAND(), RAND() from numbers(10);
+---------------------+---------------------+
| RAND() | RAND() |
+---------------------+---------------------+
| 0.36375269335997307 | 0.36375269335997307 |
| 0.09377635544457674 | 0.09377635544457674 |
| 0.8277016072035953 | 0.8277016072035953 |
| 0.4907032292521587 | 0.4907032292521587 |
| 0.8009654887127645 | 0.8009654887127645 |
| 0.158373699161646 | 0.158373699161646 |
| 0.9414531157798985 | 0.9414531157798985 |
| 0.6622571995881148 | 0.6622571995881148 |
| 0.25786072087383205 | 0.25786072087383205 |
| 0.3063851342553019 | 0.3063851342553019 |
+---------------------+---------------------+