SQL RAND Function

The RAND function returns a random number between 0 and 1.

Calling RAND with the same seed returns the same value.

Example

#

This example returns a random number between 0 and 1.

SELECT RAND() AS Number
Result:  1 record
Number
0.349414545321923

Syntax

Syntax of the RAND function.

RAND(seed)

seed -- optional, a number that indicates seed value.


More Examples

RAND multiple times

Generate a sequence of 'random' numbers with a seed value. Run it multiple times to see that the sequence is always the same.

SELECT RAND(33) AS '1',
       RAND() AS '2',
       RAND() AS '3',
       RAND() AS '4'
Result:  1 record
1 2 3 4
0.714188248293186 0.330792492424224 0.0067380347263047 0.563631043299501

Note: Running this multiple times will generate the same sequence of numbers. Only when the seed changes will you get a different sequence. Alternatively, don't include a seed and each RAND number will be different.


You may also like



Guides


vsn 3.1