Declaration
RANDOM(seed)
Description
This function generates a 64-bit integer pseudo-random value.
seedis an integer. Differentseedscan generate different random number sequences.- The
RANDOM()function uses the MT19937 pseudo-random number generation algorithm, which has a very large random space. It will not generate the same sequence again until it has generated all possible random elements, which is 219937 - 1.
Examples
The following example shows how to use RANDOM() to generate a random number.
obclient> SELECT RANDOM(4) FROM TABLE(GENERATOR(3));
+---------------------+
| RANDOM(4) |
+---------------------+
| 5267436225003336391 |
| -851690886662571060 |
| 1738617244330437274 |
+---------------------+
3 rows in set
The following example uses the result of RANDOM() as a random input for the 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 illustrates a special scenario where the seed is recalculated each time the RANDOM() parameter is set to a variable.
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