RAND() in MySQL — How It Works

What the MySQL RAND() function does, with an example.

Published October 21, 2026

RAND() returns a random float between 0 and 1. Pass a seed (RAND(42)) for a repeatable sequence.

How it works

ORDER BY RAND() LIMIT n picks random rows but is slow on big tables.

Try it in MySQL

SELECT RAND()
0.7389
SELECT FLOOR(1 + RAND() * 6)
dice roll
SELECT * FROM quotes ORDER BY RAND() LIMIT 1
random row

Practical examples

Example 1

SELECT RAND();
-- 0.7389

Example 2

SELECT FLOOR(1 + RAND() * 6);
-- dice roll

Example 3

SELECT * FROM quotes ORDER BY RAND() LIMIT 1;
-- random row

Use it in a real query

SELECT id, RAND() AS result
FROM your_table
LIMIT 10;

Common mistakes

  • ORDER BY RAND() on millions of rows.
  • Expecting cryptographic randomness.
  • Calling RAND() in WHERE and getting inconsistent results.

Key takeaways

  • RAND() in one line: RAND() returns a random float between 0 and 1.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

Fast random row?

Pick a random id range or use a precomputed random column.

Does RAND() work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles