UNIFORM
Description
Using the given random seed, uniformly sample and generate random numbers within a specific range.
Syntax
UNIFORM( <min> , <max> , <gen> )
Parameters
Parameter | Description |
---|---|
<min> | The lower limit of the random number, accepts numeric type, must be a literal |
<max> | The upper limit of the random number, accepts numeric type, must be a literal |
<gen> | Integer, random seed, commonly generated by the RANDOM function randomly. |
Return value
Return a random number within [<min>, <max>]
. If both <min>
and <max>
are integers, the return value type is BIGINT
, otherwise it is DOUBLE
.
Note that unlike Snowflake's common usage, because the RANDOM function in Doris returns a floating-point number between 0 and 1 by default, if you use RANDOM()
as a random seed, you should attach a multiplier to make the result distributed within an integer range. See the examples for details.
Example
When all input parameters are integers, return an integer:
select uniform(-100, 100, random() * 10000) as result from numbers("number" = "10");
+--------+
| result |
+--------+
| -82 |
| -79 |
| 21 |
| 19 |
| 50 |
| 53 |
| -100 |
| -67 |
| 46 |
| 40 |
+--------+
When input parameters are not all integers, return the double type:
select uniform(1, 100., random() * 10000) as result from numbers("number" = "10");
+-------------------+
| result |
+-------------------+
| 84.25057360297031 |
| 63.34296160793329 |
| 81.8770598286311 |
| 26.53334147605743 |
| 17.42787914185705 |
| 2.532901549399078 |
| 63.72223367924216 |
| 78.42165786093118 |
| 18.913688179943 |
| 41.73057334477316 |
+-------------------+
Must be literal parameters:
select uniform(1, unix_timestamp(), random() * 10000) as result from numbers("number" = "10");
ERROR 1105 (HY000): errCode = 2, detailMessage = The second parameter (max) of uniform function must be literal
Must be literal parameters:
select uniform(1, ksint, random()) from fn_test;
ERROR 1105 (HY000): errCode = 2, detailMessage = The second parameter (max) of uniform function must be literal
Fixed seeds will produce fixed results (the result of random()
is distributed between 0 and 1, and when used directly, the seed parameter of uniform
is always 0
):
select uniform(-100, 100, random()) as result from numbers("number" = "10");
+--------+
| result |
+--------+
| -68 |
| -68 |
| -68 |
| -68 |
| -68 |
| -68 |
| -68 |
| -68 |
| -68 |
| -68 |
+--------+
When any input is NULL, the output is also NULL:
select uniform(-100, NULL, random() * 10000) as result from numbers("number" = "10");
+--------+
| result |
+--------+
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL |
+--------+
select k0, uniform(0, 1000, k0) from it order by k0;
+------+----------------------+
| k0 | uniform(0, 1000, k0) |
+------+----------------------+
| NULL | NULL |
| 1 | 134 |
| 2 | 904 |
| 3 | 559 |
| 4 | 786 |
| 5 | 673 |
+------+----------------------+