Lesson

MySQL RAND() Function

10 min reading
Free Course

MySQL RAND() Function: Generating Random Floating-Point Numbers

The RAND() function returns a pseudo-random floating-point number v in the half-open range 0 <= v < 1.0. You can call it with zero arguments for an unseeded random stream, or pass an integer seed N for repeatable output.

Teams use RAND() to draw A/B test samples, assign random voucher offsets, or pick small random rows. Knowing how MySQL evaluates RAND() per row and handles seeds keeps queries fast and bug-free.

Syntax

RAND()
RAND(seed)
  • seed: Optional integer expression. Passing the same constant integer seed repeats the exact generated sequence within a query or connection.

Practical Example

Suppose demo_products contains promotional items, and you want to assign each SKU a repeatable pseudo-random audit score between 1 and 100 using a fixed seed (42) for reproducible QA testing.

SELECT
    product_id,
    product_name,
    FLOOR(1 + RAND(42) * 100) AS audit_score_seeded,
    ROUND(RAND(NULL), 4) AS unseeded_even_with_null
FROM demo_products
ORDER BY product_id
LIMIT 3;
product_id product_name audit_score_seeded unseeded_even_with_null
1 Mechanical Keyboard 64 0.4182
2 Ergonomic Mouse 19 0.8731
3 Monitor Arm 72 0.1059

Notice RAND(42) seeds the generator once for the expression so rows 1, 2, and 3 receive repeatable sequence numbers across runs.

Common Mistakes & NULL Behavior

  • ORDER BY RAND() performance trap: Writing SELECT * FROM demo_products ORDER BY RAND() LIMIT 5 forces MySQL to evaluate RAND() for every row, write a temporary table, and run a full filesort. On tables with hundreds of thousands of rows, this creates severe CPU and I/O spikes. Instead, sample random primary-key IDs in application code or join against a random offset range.
  • RAND(NULL) does NOT return NULL: Unlike almost every other MySQL function, RAND(NULL) ignores the NULL seed and still generates a fresh random float in [0, 1).
  • Using RAND() inside WHERE: WHERE id = FLOOR(1 + RAND() * 10) evaluates RAND() independently for every scanned row, so it can return zero rows or multiple rows unpredictably.

Frequently Asked Questions

How do I generate a random integer between A and B inclusive?

Use FLOOR(A + RAND() * (B - A + 1)). For example, FLOOR(1 + RAND() * 6) simulates a fair six-sided die roll.

Does RAND(seed) stay identical across multiple queries?

Within a single query, RAND(N) seeds that expression predictably. Calling RAND(N) in the SELECT list and again in ORDER BY creates two independent generators unless you alias the column.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum