Skip to content

Latest commit

 

History

History
150 lines (107 loc) · 4.07 KB

rand-transact-sql.md

File metadata and controls

150 lines (107 loc) · 4.07 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
RAND (Transact-SQL)
Returns a pseudo-random float value from 0 through 1, exclusive.
MikeRayMSFT
mikeray
randolphwest
02/16/2025
sql
t-sql
reference
RAND
RAND_TSQL
RAND function
values [SQL Server], random float
random float value
TSQL
=azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current

RAND (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw]

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

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

RAND ( [ seed ] )

Arguments

seed

An integer expression (tinyint, smallint, or int) that gives the seed value. If seed isn't specified, the [!INCLUDE ssDEnoversion] 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