MariaDB RAND() Function

In MariaDB, RAND() is a built-in function that returns a random number between 0 (inclusive) and 1 (exclusive).

MariaDB RAND() Syntax

Here is the syntax of the MariaDB RAND() function:

RAND()
RAND(seed)

Parameters

seed

Optional. The seed to generate random numbers. If provided seed, the RAND(seed) function will generate a repeatable sequence of random numbers.

If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'RAND'.

Return value

The MariaDB RAND() function returns a random number between 0 (inclusive) and 1 (exclusive).

If you provide an parameter seed, the MariaDB RAND(seed) function will generate a repeatable sequence of random numbers.

RAND() returns a random decimal between 0 and 1. If you want to return a random number in other intervals (such as m and n), please use the following formula:

  • A random number between m and n: RAND() * (n - m) + m
  • A random integer between m and n: FLOOR(RAND() * (n - m + 1) + m)

MariaDB RAND() Examples

Returns random numbers between 0 and 1

SELECT
    RAND(),
    RAND(),
    RAND(),
    RAND()\G

Output:

RAND(): 0.24013628460479555
RAND(): 0.9220884246026058
RAND(): 0.8900332999322873
RAND(): 0.6839012565872644

Returns random numbers between 10 and 20

SELECT
    RAND() * (20 - 10) + 10,
    RAND() * (20 - 10) + 10,
    RAND() * (20 - 10) + 10,
    RAND() * (20 - 10) + 10\G

Output:

RAND() * (20 - 10) + 10: 17.494064138731048
RAND() * (20 - 10) + 10: 16.95328330337376
RAND() * (20 - 10) + 10: 12.284224408012093
RAND() * (20 - 10) + 10: 10.56127243727564

Returns random integers between 10 and 20

SELECT
    FLOOR(RAND() * (20 - 10 + 1) + 10),
    FLOOR(RAND() * (20 - 10 + 1) + 10),
    FLOOR(RAND() * (20 - 10 + 1) + 10),
    FLOOR(RAND() * (20 - 10 + 1) + 10)\G

Output:

FLOOR(RAND() * (20 - 10 + 1) + 10): 16
FLOOR(RAND() * (20 - 10 + 1) + 10): 18
FLOOR(RAND() * (20 - 10 + 1) + 10): 12
FLOOR(RAND() * (20 - 10 + 1) + 10): 19

Seed

The MariaDB RAND() function with a seed return a repeatable sequence of random numbers:

SELECT
    RAND(5),
    RAND(5),
    RAND(5)\G

Output:

RAND(5): 0.40613597483014313
RAND(5): 0.40613597483014313
RAND(5): 0.40613597483014313

Conclusion

In MariaDB, RAND() is a built-in function that returns a random number between 0 (inclusive) and 1 (exclusive).