| title | RAND (Transact-SQL) | |||
|---|---|---|---|---|
| description | Returns a pseudo-random float value from 0 through 1, exclusive. | |||
| author | MikeRayMSFT | |||
| ms.author | mikeray | |||
| ms.reviewer | randolphwest | |||
| ms.date | 07/28/2023 | |||
| ms.service | sql | |||
| ms.subservice | t-sql | |||
| ms.topic | reference | |||
| f1_keywords |
|
|||
| helpviewer_keywords |
|
|||
| dev_langs |
|
|||
| monikerRange | =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current |
[!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
RAND ( [ seed ] )
[!INCLUDE sql-server-tsql-previous-offline-documentation]
Note
[!INCLUDE synapse-analytics-od-unsupported-syntax]
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.
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 ofRAND()produce results based on the seededRAND()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 anUPDATEorINSERTquery, all affected rows get the same value.
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