UNIFORM
Syntax
UNIFORM(<min> , <max> , <gen>)
Description
The UNIFORM() function returns an integer or a float that follows a uniform distribution.
- The
minandmaxparameters must be scalar values that do not change with row iteration. For example, constants or scalar functions. In PL, they can also be expressions like@v1or1+@v3. - In MySQL mode, if both
minandmaxare integers, the output of the UNIFORM() function is an integer; otherwise, the output is a float. genis a numerical generation function, typically using theRANDOM()function. If the input value is a constant, the output of the UNIFORM() function is also a constant.
Examples
The following example generates an integer between 1 and 10 uniformly.
SELECT v, COUNT(*) FROM (SELECT UNIFORM(1, 10, RANDOM()) v FROM TABLE(GENERATOR(1000000))) x GROUP BY v ORDER BY v;
+------+----------+
| v | count(*) |
+------+----------+
| 1 | 100738 |
| 2 | 100119 |
| 3 | 100223 |
| 4 | 99537 |
| 5 | 100125 |
| 6 | 100001 |
| 7 | 100275 |
| 8 | 99235 |
| 9 | 99837 |
| 10 | 99910 |
+------+----------+
10 rows in set (0.002 sec)
The following example shows that the return type is related to the parameter type.
SELECT UNIFORM(0.0, 10, RANDOM()) FROM TABLE(GENERATOR(4)) ORDER BY 1;
+----------------------------+
| UNIFORM(0.0, 10, RANDOM()) |
+----------------------------+
| 2.3520877625884653 |
| 4.155845987385725 |
| 8.323930497420852 |
| 9.844002748532109 |
+----------------------------+
4 rows in set (0.001 sec)
SELECT UNIFORM(0, 10, RANDOM()) FROM TABLE(GENERATOR(4)) ORDER BY 1;
+--------------------------+
| UNIFORM(0, 10, RANDOM()) |
+--------------------------+
| 0 |
| 4 |
| 8 |
| 9 |
+--------------------------+
4 rows in set (0.001 sec)