Function Reference

RAND

PostgreSQL equivalent:

Returns a random floating-point number from 0 to 1.

MySQL 8.1
RAND([seed])

Parameters

seedINToptional
Generator seed: the same seed gives the same sequence

Return value

A floating-point number (DOUBLE) from 0 inclusive to 1 exclusive

Examples

Random number

MySQL 8.1
SELECT RAND();
RAND()
0.6515579350772853

Repeatable number with a seed

MySQL 8.1
SELECT RAND(1);
RAND(1)
0.40540353712197724

Random integer from 1 to 6

MySQL 8.1
SELECT FLOOR(1 + RAND() * 6);
FLOOR(1 + RAND() * 6)
1

Random table row

MySQL 8.1
SELECT member_name
FROM FamilyMembers
ORDER BY RAND()
LIMIT 1;
member_name
Ernest Forrest

Details

A random integer from a to b inclusive is given by FLOOR(a + RAND() * (b - a + 1)). ORDER BY RAND() together with LIMIT picks random table rows.

See also