RAND | Syntax: | RAND([Number]) | Return value: | FLOAT | Function type: | Numeric function | |
|
|
The RAND() function returns a random floating point value with double precision from 0 to 1.
If "Number" is specified, it is used as the seed that produces a repeatable sequence of values for multi-row queries.
If the value of "Number" is NULL, the same value is returned as with RAND(0). |
SQL Examples for the RAND function |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
|
select rand();
select rand();
select rand(4);
select rand(4);
select rand(0);
select rand(null);
select id, rand() from menu limit 3;
select id, rand() from menu limit 3;
select id, rand(4) from menu limit 3;
select id, rand(4) from menu limit 3;
|
|
rand() |
double(23) |
0.6991170977234068 |
|
|
rand() |
double(23) |
0.7097095229753382 |
|
|
rand(4) |
double(23) |
0.15595286540310166 |
|
|
rand(4) |
double(23) |
0.15595286540310166 |
|
|
rand(0) |
double(23) |
0.15522042769493574 |
|
|
rand(null) |
double(23) |
0.15522042769493574 |
|
|
id | |
|
rand() | |
|
menu tinyint(4) | double(23) |
1 | 0.4511963524401154 |
2 | 0.12685322400820742 |
3 | 0.2806770934543359 |
|
|
|
id | |
|
rand() | |
|
menu tinyint(4) | double(23) |
1 | 0.022825825980702253 |
2 | 0.27209788027414855 |
3 | 0.292011954162281 |
|
|
|
id | |
|
rand(4) | |
|
menu tinyint(4) | double(23) |
1 | 0.15595286540310166 |
2 | 0.6238114970026645 |
3 | 0.6511989195376624 |
|
|
|
id | |
|
rand(4) | |
|
menu tinyint(4) | double(23) |
1 | 0.15595286540310166 |
2 | 0.6238114970026645 |
3 | 0.6511989195376624 |
|
|
|
|
The examples were created with the MyWAY SQL manager: |
How to use the RAND() function in MySQL and MariaDB databases |
The RAND() function in MySQL and MariaDB is commonly used when there is a need to generate random values for testing, simulation, random sampling, or other scenarios where randomness is required in SQL queries. To generate random numbers within a certain range, the RAND() function can be combined with mathematical operations. |
|
Further MySQL und MariaDB SQL Numeric functions |
|