RAND (Transact-SQL)

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics

Returns a pseudo-random float value from 0 through 1, exclusive.

Transact-SQL syntax conventions

Syntax

RAND ( [ seed ] )  

Note

To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

Note

This syntax is not supported by serverless SQL pool in Azure Synapse Analytics.

Arguments

seed
Is an integer expression (tinyint, smallint, or int) that gives the seed value. If seed is not specified, the SQL Server Database Engine assigns a seed value at random. For a specified seed value, the result returned is always the same.

Return Types

float

Remarks

Repetitive calls of RAND() with the same seed value return the same results.

For one connection, if RAND() is called with a specified seed value, all subsequent calls of RAND() produce results based on the seeded RAND() call. For example, the following query will always return the same sequence of numbers.

SELECT RAND(100), RAND(), RAND()   

Examples

The following example produces four different random numbers that are generated by the RAND function.

DECLARE @counter SMALLINT;  
SET @counter = 1;  
WHILE @counter < 5  
   BEGIN  
      SELECT RAND() Random_Number  
      SET @counter = @counter + 1  
   END;  
GO  

See Also

Mathematical Functions (Transact-SQL)