Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
RAND ( [ seed ] )
Note
This syntax is not supported by serverless SQL pool in Azure Synapse Analytics.
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.
float
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.
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
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
.
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
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
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
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register today