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).
| seq | SQL Server RAND | XLDB RAND |
|---|---|---|
| 1 | 0.717683577913778 | 0.320075967691334 |
| 2 | 0.717683577913778 | 0.242518944291069 |
| 3 | 0.717683577913778 | 0.939540304240701 |
| 4 | 0.717683577913778 | 0.995485696135315 |
| 5 | 0.717683577913778 | 0.390991905650971 |
| 6 | 0.717683577913778 | 0.847310724479799 |
| 7 | 0.717683577913778 | 0.0338573758405467 |
| 8 | 0.717683577913778 | 0.162934983399244 |
| 9 | 0.717683577913778 | 0.407146586492377 |
| 10 | 0.717683577913778 | 0.186449045740706 |
| 11 | 0.717683577913778 | 0.252528679920915 |
| 12 | 0.717683577913778 | 0.942039038585549 |
| 13 | 0.717683577913778 | 0.857604925612895 |
| 14 | 0.717683577913778 | 0.957765087979192 |
| 15 | 0.717683577913778 | 0.442119654211433 |
See Also
RANDBETWEEN - Calculate a random integer number between the numbers you specify.