RAND

RAND()

Description

RAND() function is a non-deterministic random number generation function that can return a random floating-point number in the range [0, 1), with a uniform distribution. This function has a wide range of applications in various random sampling, simulation, and probability calculation scenarios.

Return Type

This function returns a value of type DOUBLE.

Usage Example

  1. Generate a random number:
SELECT RAND(); -- Returns a random floating-point number in the range [0, 1)

Result Example:

0.53298387155464
  1. Generate a random number for each row in a table:
    SELECT id, RAND() AS random_number FROM users;

Result Example:

1, 0.3245897911074116
2, 0.6547382678236785
3, 0.12345678903485962
  1. Use with other numerical functions to generate a random number within a specified range:
SELECT (RAND() * 100) AS random_value BETWEEN 0 AND 100; -- Generate a random number between 0 and 100

Result Example:

67.39123456
  1. Used for randomly selecting records:
SELECT * FROM products ORDER BY RAND() LIMIT 1; -- Randomly select a product record

Result Example:

id: 3,
name: 'Randomly Selected Product',
price: 19.99

Notes

  • The RAND() function generates a new random number each time it is called.
  • If you need repeatable random results, you can use RAND(seed), where seed is an integer used to initialize the random number generator's seed. This way, the sequence of random numbers generated can remain consistent across multiple queries. For example:
SELECT RAND(1); -- Initialize the random number generator using seed 1
  • In some database management systems, the RAND() function may have different behaviors or limitations. Please refer to the relevant documentation for the specific system.