RAND

RAND is a built-in function in SQL that generates a random float value from 0 to 1. It can be used whenever a random number need to be generated in SQL procedures or calculations.

RAND([seed])

  • seed: This optional parameter is a positive integer that determines the start point from which the RAND() function generates a random number sequence. If the seed value is provided, the function will produce a predictable sequence of random numbers each time the MySQL server starts. However, if this seed parameter is not specified, RAND() will produce a new random number on each call.

Example

SELECT RAND();

Output

+------------------+
| RAND() |
+------------------+
|0.734526838547049 |
+------------------+

Explanation

The RAND() function in MySQL returns a random floating-point value between 0 and 1. In the provided example, the RAND() function has returned 0.734526838547049 as a random number. Each execution of the RAND() function may return a different number as it generates a random float value.

RAND( [ seed ] )

  • seed: This optional parameter is an integer value to initialize the random number generator. Each specific seed value will always return the same sequence of random numbers. If the seed is not specified, SQL Server will generate a different sequence of random numbers each time the RAND() function is called.

Example

SELECT RAND() AS RandomNumber;

Output

RandomNumber
0.349305484684758

Explanation

RAND() is a function in SQL Server that generates a new random float value between 0 and 1 every time a new query is executed. In this example, it has returned 0.349305484684758 which is a random number in the range [0, 1).

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.