Logo

SQL Server RANDNORM Function

Updated 2023-10-18 16:07:18.233000

Description

Use the scalar function RANDNORM to calculate a pseudo-random number based on the normal distribution.

Syntax

SELECT [westclintech].[wct].[RANDNORM](
  <@mu, float,>
 ,<@sigma, float,>)

Arguments

@mu

is the mean of a normal distribution. @mu is an expression of type float or of a type that can be implicitly converted to float.

@sigma

is the standard deviation a normal distribution. @sigma is an expression of type float or of a type that can be implicitly converted to float.

Return Type

float

Remarks

RANDNORM is non-deterministic, so results will vary.

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

To generate a series of pseudo-random normal numbers, consider using the SeriesFloat function.

To generate a pseudo-random number between zero and one, consider using the XLeratorDB RAND function.

To generate a pseudo-random integer between two integers, consider using the RANDBETWEEN function.

If @mu is NULL it will be set to zero.

If @sigma is NULL it will be set to one.

Examples

This example demonstrates the difference between using the built-in SQL Server function RAND and the XLeratorDB RANDNORM 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.randnorm(NULL, NULL) as [XLDB RANDNORM]
from mycte;

This produces the following result.

seqSQL Server RANDXLDB RANDNORM
10.7861856552854110.132515769270168
20.7861856552854110.55422759842865
30.7861856552854110.00279640603651261
40.7861856552854111.20615188572666
50.7861856552854110.258279276822493
60.7861856552854110.328259483474435
70.7861856552854110.0704535933678625
80.786185655285411-0.325716938056977
90.786185655285411-0.276812639860543
100.7861856552854110.487789164610815
110.7861856552854110.303768736287741
120.7861856552854110.79775224936946
130.7861856552854110.478204215978753
140.7861856552854110.745115412390468
150.786185655285411-0.561231528988071

Your results will be different.

In this example we will generate 4 random numbers for each row.

with mycte
as (select 1 as seq
    union all
    select seq + 1
    from mycte
    where seq < 15)
select seq,
       wct.RANDNORM(NULL, NULL) as [A],
       wct.RANDNORM(100, 15) as [B],
       wct.RANDNORM(72, 2) as [C],
       wct.RANDNORM(50000, 15000) as [D]
from mycte;

This produces the following result.

seqABCD
1-0.37438455959187112.19701213790470.046058633476849452.9037458025
20.3524256269304796.277193537139972.01751937878752025.4333293082
30.5818715975727093.801020447943273.648475103642766384.5057201813
4-0.24507083494989100.55244246946872.396899475081149969.1110176925
50.67990016539522103.49254468723669.774342561362849654.2304364637
6-0.94473559022718101.76023329485371.103051317757242593.7391076126
7-0.57363162397797100.056396562966.438019580188649856.9245265609
8-0.0818058292752787.361539503976172.696299090713658191.1279599142
91.009268042942580.845795048096973.871003036107646528.7518703674
102.66764331693742118.63917207751272.544492000089680447.1037280331
110.12854384378103104.83793204032774.0798610244449109923.572201717
12-0.7708625741175496.357913909110671.894029967282656044.7478108594
130.37903147013382134.37862012630571.555168646021239432.7239847204
14-0.0779049030086387.761353854781470.99416788653247844.105807051
15-0.59210431507451103.67193219014573.85265071267144921.2034130292

Your results will be different.