Syntax
UNIFORM(<min> , <max> , <gen>)
Purpose
UNIFORM() returns a uniformly distributed integer or floating-point number.
minandmaxmust be scalar values that do not change with row iteration. For example, they can be constants and scalar functions. In PL, they can also be@v1and1+@v3.- In MySQL mode, if
minormaxis an integer, the output of theUNIFORM()function is an integer; otherwise, the output of theUNIFORM()function is a floating-point number. genis a numeric value generation function. Generally, theRANDOM()function is used. If the input value is a constant,UNIFORM()returns a fixed value.
Examples
The following example generates uniformly distributed integers between 1 and 10.
obclient> 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
The following example shows that the data type of the result returned by UNIFORM() is related with the type of its parameters.
obclient> 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
obclient> SELECT UNIFORM(0, 10, RANDOM()) FROM TABLE(GENERATOR(4)) ORDER BY 1;
+--------------------------+
| UNIFORM(0, 10, RANDOM()) |
+--------------------------+
| 0 |
| 4 |
| 8 |
| 9 |
+--------------------------+
4 rows in set