Rand Function In Sql in SQL
RAND Function in SQL
The RAND() function generates a random floating-point number between 0 (inclusive) and 1 (exclusive).
Syntax:
RAND()You can optionally provide a seed to generate repeatable sequences:
RAND(seed)seed— an integer value to initialize the random number generator.
Example:
Generate a random number between 0 and 1:
SELECT RAND() AS random_value;Example with Seed:
SELECT RAND(123) AS random_value;Calling RAND(123) multiple times will generate the same sequence of numbers.
Notes:
Supported in MySQL, SQL Server, and others.
To get a random number within a range, multiply and add accordingly. For example, random integer between 1 and 10 in MySQL:
SELECT FLOOR(1 + RAND() * 10) AS random_int;Want examples on how to use RAND() for selecting random rows or other use cases?