RAND
Syntax
Description
Returns a random [DOUBLE](../../../../data-types/data-types-numeric-data-types/double.md)
precision floating point value v in the range 0 <= v < 1.0. If
a constant integer argument N is specified, it is used as the seed
value, which produces a repeatable sequence of column values. In the example below, note that the sequences of values produced by
RAND(3) is the same both places where it occurs.
In a WHERE clause, RAND() is evaluated each time the WHERE is executed.
Statements using the RAND() function are not safe for statement-based replication.
Practical uses
The expression to get a random integer from a given range is the following:
RAND() is often used to read random rows from a table, as follows:
Note, however, that this technique should never be used on a large table as it will be extremely slow. MariaDB will read all rows in the table, generate a random value for each of them, order them, and finally will apply the LIMIT clause.
Examples
Using the same seed, the same sequence will be returned:
Generating a random number from 5 to 15:
See Also
This page is licensed: GPLv2, originally from fill_help_tables.sql
Last updated
Was this helpful?