RAND() function :
This function in SQL Server is used to return a random decimal value and this value lies in the range greater than and equal to zero (>=0) and less than 1. If we want to obtain a random integer R in the range i <= R < j, we have to use the expression “FLOOR(i + RAND() * (j − i))”.
Features :
- This function is used to give a random decimal value.
- The returned value lies in between 0 (inclusive) and 1 (exclusive).
- If this function does not accept any parameter, it will returns a completely random number.
- If this function takes a parameter, it will returns a repeatable sequence of random numbers.
- This function accepts optional parameter.
- This function uses a formula
- “FLOOR(i + RAND() * (j − i))” to get a random integer R,
- where R lies in the range of “i <= R < j”.
Syntax :
RAND(N)
Parameter :
This method accepts a parameter as given below :
- N : If N is specified, it returns a repeatable sequence of random numbers. If no N is specified, it returns a completely random number. It is optional and it works as a seed value.
Returns :
It returns a random number between 0 (inclusive) and 1 (exclusive).
Example:
Getting a random value between 0 and 1.
SELECT RAND();
Output :
0.37892290119984562