Syntax
RANDOM(seed)
Purpose
RANDOM() generates a pseudo-random number that is a 64-bit integer.
seedis an integer. The generated array of random numbers varies with the value ofseed.RANDOM()uses the MT19937 algorithm to generate pseudo-random numbers. This algorithm ensures a wide range for random numbers and generates a repeated array only after generating 219937 - 1 random elements.
Examples
The following example uses RANDOM() to generate random numbers.
obclient> SELECT RANDOM(4) FROM TABLE(GENERATOR(3));
+---------------------+
| RANDOM(4) |
+---------------------+
| 5267436225003336391 |
| -851690886662571060 |
| 1738617244330437274 |
+---------------------+
3 rows in set
The following example uses the result of the RANDOM() function as a random input for a distribution function.
obclient> SELECT UNIFORM(1, 100, RANDOM()) FROM TABLE(GENERATOR(4));
+---------------------------+
| UNIFORM(1, 100, RANDOM()) |
+---------------------------+
| 66 |
| 71 |
| 52 |
| 14 |
+---------------------------+
4 rows in set
The following example shows a special scenario where the input parameter of RANDOM() is a variable and the value of seed is recalculated each time.
obclient> SELECT * FROM t1;
+------+
| c1 |
+------+
| 3 |
| 4 |
| 5 |
| 1 |
| 1 |
+------+
5 rows in set
obclient> SELECT t1.c1, RAND(t1.c1), RANDOM(t1.c1) FROM TABLE(GENERATOR(1)), t1;
+------+---------------------+----------------------+
| c1 | RAND(t1.c1) | RANDOM(t1.c1) |
+------+---------------------+----------------------+
| 3 | 0.9057697559760601 | 1084041170817055659 |
| 4 | 0.15595286540310166 | 5267436225003336391 |
| 5 | 0.40613597483014313 | 3192483991702052534 |
| 1 | 0.40540353712197724 | -6753783847308464280 |
| 1 | 0.40540353712197724 | -6753783847308464280 |
+------+---------------------+----------------------+
5 rows in set