Logo

SQL Server RAND Function

Updated 2023-10-18 15:26:43.480000

Description

Use the scalar function RAND to calculate a random number greater than or equal to zero and less than one.

Syntax

SELECT [westclintech].[wct].[RAND] ()

Arguments

Return Type

float

Remarks

Unlike the built-in SQL Server RAND function, RAND is evaluated for every row in the resultant table.

Examples

Compare the SQL Server RAND() function to the XLeratorDB RAND() function.

with mycte as
(
      select 1 as seq
      union all
      select seq + 1
      from mycte
      where seq < 15
)
      select seq
      ,rand() as [SQL Server RAND]
      ,wct.rand() as [XLDB RAND]
      from mycte;

Here is the result set (your results will be different).

seqSQL Server RANDXLDB RAND
10.7176835779137780.320075967691334
20.7176835779137780.242518944291069
30.7176835779137780.939540304240701
40.7176835779137780.995485696135315
50.7176835779137780.390991905650971
60.7176835779137780.847310724479799
70.7176835779137780.0338573758405467
80.7176835779137780.162934983399244
90.7176835779137780.407146586492377
100.7176835779137780.186449045740706
110.7176835779137780.252528679920915
120.7176835779137780.942039038585549
130.7176835779137780.857604925612895
140.7176835779137780.957765087979192
150.7176835779137780.442119654211433

See Also

RANDBETWEEN - Calculate a random integer number between the numbers you specify.

RANDNORM - Calculate a pseudo-random number based on a normal distribution for the given mean and standard deviation.

SERIESFLOAT - Generate a range of floating point values.