Skip to content

Latest commit

 

History

History
76 lines (59 loc) · 2.49 KB

File metadata and controls

76 lines (59 loc) · 2.49 KB
title RAND (Transact-SQL)
description RAND (Transact-SQL)
author MikeRayMSFT
ms.author mikeray
ms.reviewer
ms.date 03/06/2017
ms.prod sql
ms.prod_service synapse-analytics, database-engine, sql-database
ms.technology t-sql
ms.topic reference
ms.custom
f1_keywords
RAND
RAND_TSQL
helpviewer_keywords
RAND function
values [SQL Server], random float
random float value
dev_langs
TSQL
monikerRange = 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.

Topic link icon Transact-SQL Syntax Conventions

Syntax

RAND ( [ seed ] )  

[!INCLUDEsql-server-tsql-previous-offline-documentation]

Arguments

seed
Is an integer expression (tinyint, smallint, or int) that gives the seed value. If seed is not specified, the [!INCLUDEssDEnoversion] 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 will always return the same sequence of numbers.

SELECT RAND(100), RAND(), RAND()   

Examples

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

Mathematical Functions (Transact-SQL)