Skip to main content

RANDOM

Description​

Returns a random number between 0 and 1, or returns the required random number according to the parameters.

  • Note: All parameters must be constants.

Alias​

  • RAND

Syntax​

RANDOM() -- Generates a random number between 0 and 1

RANDOM(<seed>) -- Generates a fixed sequence of random numbers between 0 and 1 based on the seed value

RANDOM(<a> , <b>) -- Generates a random number between a and b

Parameters​

ParameterDescription
<seed>random number generator seed. Returns a fixed sequence of random numbers between 0 and 1.
<a>The lower bound of a random number.
<b>The upper bound of a random number. It must be less than the lower bound.

Return value​

  • If no parameters are passed: Returns a random number between 0 and 1.

  • If a single parameter seed is passed: Returns a fixed sequence of random numbers between 0 and 1.

  • If two parameters a and b are passed: Returns a random integer between a and b.

Example​

select random();
+--------------------+
| random() |
+--------------------+
| 0.8047437125910604 |
+--------------------+
select rand(1.2);
+---------------------+
| rand(1) |
+---------------------+
| 0.13387664401253274 |
+---------------------+
select rand(-20, -10);
+------------------+
| random(-20, -10) |
+------------------+
| -10 |
+------------------+