Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

RAND (Transact-SQL)

Applies to: 👁 Image
SQL Server 👁 Image
Azure SQL Database 👁 Image
Azure SQL Managed Instance 👁 Image
Azure Synapse Analytics 👁 Image
SQL database in Microsoft Fabric

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

👁 Image
Transact-SQL syntax conventions

Syntax

RAND ( [ seed ] )

Note

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

Arguments

seed

An integer expression (tinyint, smallint, or int) that gives the seed value. If seed isn't 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 always returns the same sequence of numbers.

    SELECT RAND(100),
     RAND(),
     RAND();
    
  • When you use the RAND() function in an UPDATE or INSERT query, all affected rows get the same value.

Examples

A. Create four different random numbers

The following example produces four different random numbers generated by the RAND() function.

DECLARE @counter AS SMALLINT;

SET @counter = 1;

WHILE @counter < 5
 BEGIN
 SELECT RAND() AS RandomNumber;
 SET @counter = @counter + 1;
 END
GO

B. Randomly generate 0 or 1

The following example uses the ROUND() function to randomly return either 0 or 1.

SELECT ROUND(RAND(), 0) AS ZeroOrOne;
GO

Note

In this example, the FLOOR() function always returns 0, whereas the CEILING() function always returns 1.

C. Generate random numbers greater than 1

You can scale the range of the random float by multiplying the RAND() function with an integer value. The following example returns a random float between 0 and 10.

DECLARE @Constant AS INT;
SET @Constant = 10;

SELECT @Constant * RAND() AS RandomNumber;
GO

D. Generate random integers

You can generate a random integer value by scaling the RAND() function and combining it with the ROUND(), FLOOR(), or CEILING() functions. The following example returns a random integer between 0 and 10, inclusive.

DECLARE @Constant AS INT;
SET @Constant = 10;

SELECT ROUND(@Constant * RAND(), 0) AS FirstRandomInteger,
 FLOOR(@Constant * RAND()) AS SecondRandomInteger,
 CEILING(@Constant * RAND()) AS ThirdRandomInteger;
GO

E. Insert random values into a table

The following example populates a table variable with random int, float, and bit values.

DECLARE @RandomTable TABLE (
 RandomIntegers INT,
 RandomFloats FLOAT,
 RandomBits BIT);

DECLARE @RowCount AS INT;
DECLARE @Counter AS INT;

SET @RowCount = 10;
SET @Counter = 1;

WHILE @Counter <= @RowCount
 BEGIN
 INSERT INTO @RandomTable
 VALUES (ROUND(10 * RAND(), 0), RAND(), CAST (ROUND(RAND(), 0) AS BIT));
 SET @Counter = @Counter + 1;
 END

SELECT *
FROM @RandomTable;
GO

Related content


Feedback

Was this page helpful?

Additional resources