Skip to content

Latest commit

 

History

History
81 lines (57 loc) · 2.34 KB

rand-transact-sql.md

File metadata and controls

81 lines (57 loc) · 2.34 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
07/28/2023
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

Is 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

The following example produces four different random numbers 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