Syntax
RANDOM(seed)
Purpose
This function generates a 64-bit pseudo-random integer.
- The
seedparameter is an integer. Differentseedvalues generate different random number sequences. - The
RANDOM()function uses the MT19937 pseudo-random number generation algorithm, which has an extremely large random space. It generates 219937 - 1 random elements before repeating the same sequence.
Examples
The following example demonstrates how to use the RANDOM() function to generate random numbers.
obclient> SELECT RANDOM(4) FROM TABLE(GENERATOR(3));
+---------------------+
| RANDOM(4) |
+---------------------+
| 5267436225003336391 |
| -851690886662571060 |
| 1738617244330437274 |
+---------------------+
3 rows in set
The following example shows how to use 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 demonstrates a special scenario where, when the RANDOM() function's parameter is a variable, the 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
