RAND()
This page is part of MariaDB's Documentation.
The parent of this page is: Functions for MariaDB Xpand
Topics on this page:
Overview
Returns a random non-negative floating-point number less than 1.
USAGE
RAND()
DETAILS
RAND()
is a mathematical function that returns a random non-negative floating-point number less than 1.
To generate a random integer value in the range x
.. y
(inclusive) you can multiply the returned value by (y - x + 1)
, convert the value to an integer, and add x
.
EXAMPLES
This generates a random integer value that ranges from 1 to 5 (inclusive):
SELECT FLOOR(RAND()*5) + 1 AS '1st run';
+---------+
| 1st run |
+---------+
| 2 |
+---------+
SELECT FLOOR(RAND()*5) + 1 AS '2nd run';
+---------+
| 2nd run |
+---------+
| 5 |
+---------+
CREATE TABLE rand_example (
x VARCHAR(10)
);
INSERT INTO rand_example VALUES
('one'), ('two'), ('three');
SELECT x AS '1st run'
FROM rand_example
ORDER BY RAND();
+---------+
| 1st run |
+---------+
| two |
| one |
| three |
+---------+
SELECT x AS '2nd run'
FROM rand_example
ORDER BY RAND();
+---------+
| 2nd run |
+---------+
| one |
| three |
| two |
+---------+