Home > T-SQL > SQL SERVER – Pseudo Random Number function RAND() – SQL Math Functions @SQLSERVER

## SQL SERVER – Pseudo Random Number function RAND() – SQL Math Functions @SQLSERVER

RAND() function returns a pseudo-random float value. This function can be called with argument or without argument. This argument refers to a seed value of the rand number. The seed value type may be any of these data types (tinyint, smallint or int).

The output value of rand() function  is the range from 0 through 1 float value.

What is pseudo-Random number?

The random numbers used in computer programs are pseudo-random, which means they are generated in a predictable fashion using a mathematical formula. This is fine for many purposes, but it may not be random in the way you expect if you’re used to dice rolls and lottery drawings. – Random.org

Syntax

RAND ( [ seed ] )

Example

select rand()
Output: ???? execute this code in your SSMS.

select rand(100)
Output: ???? execute this code in your SSMS.

T-SQL code to generate random number between 0 to 9

declare @i as int = 1while @i <= 10
begin
print cast(@i as char(2)) +’ Rand() = ‘+  cast(cast(rand() * 10 as tinyint) as varchar(1))
set @i = @i + 1
end
Output:
1  Rand() = 9
2  Rand() = 4
3  Rand() = 4
4  Rand() = 7
5  Rand() = 6
6  Rand() = 0
7  Rand() = 2
8  Rand() = 8
9  Rand() = 8
10 Rand() = 0

Random numbers can be used to build puzzle games and can be used in the mathematical calculation to find unpredictable values.

Now, you can play with Rand() function in your application.