 # SQL Server random normal distribution

RANDNORMAL

Updated: 31 March 2014

Use the table-valued function RANDNORMAL to generate a sequence of random numbers from the normal distribution with mean @mu and standard deviation @sigma.
Syntax
SELECT * FROM [wctMath].[wct].[RANDNORMAL](
<@Rows, int,>
,<@mu, float,>
,<@sigma, float,>)
Arguments
@Rows
the number of rows to generate. @Rows must be of the type int or of a type that implicitly converts to int.
@mu
the mean of the distribution. @mu must be of the type float or of a type that implicitly converts to float.
@sigma
the standard deviation of the distribution. @sigma must be of the type float or of a type that implicitly converts to float.
Return Types
RETURNS TABLE (
[Seq] [int] NULL,
[X] [float] NULL
)
Remarks
·         @sigma must be greater than zero.
·         If @mu is NULL then @mu is set to zero.
·         If @sigma is NULL then @sigma is set to 1.
·         If @Rows is less than 1 then no rows are returned.
Examples
In this example we create a sequence 1,000,000 truncated random numbers from a standard normal distribution and COUNT the results. Elementary statistics leads us to expect the results to be distributed approximately like this:

 X COUNT -4 31 -3 1318 -2 21400 -1 135905 0 682689 1 135905 2 21400 3 1318 4 31

SELECT
X,
COUNT(*)
FROM (
SELECT
wct.TRUNC(x,0) as x
FROM
wct.RANDNORMAL(
1000000, --@Rows
NULL,    --@mu
NULL     --@sigma
)
)n
GROUP BY
X
ORDER BY
1

This produces the following result. Your results will be different.

 X COUNT -4 40 -3 1315 -2 21185 -1 136077 0 682787 1 135712 2 21556 3 1289 4 39

In this example we generate 1,000,000 random numbers from a normal distribution with a mean of 100 and a standard deviation of 15. We calculate the mean, standard deviation, skewness, and excess kurtosis from the resultant table and compare those values to the expected values for the distribution.
DECLARE @size as int = 1000000
DECLARE @mu as float = 100
DECLARE @sigma as float = 15
DECLARE @mean as float = @mu
DECLARE @var as float = POWER(@mu, 2)
DECLARE @stdev as float = @sigma
DECLARE @skew as float = 0
DECLARE @kurt as float = 0

SELECT
stat,
[RANDNORMAL],
[EXPECTED]
FROM (
SELECT
x.*
FROM (
SELECT
AVG(x) as mean_NORMAL,
STDEVP(x) as stdev_NORMAL,
wct.SKEWNESS_P(x) as skew_NORMAL,
wct.KURTOSIS_P(x) as kurt_NORMAL
FROM
wct.RANDNORMAL(@size,@mu,@sigma)
)n
CROSS APPLY(
VALUES
('RANDNORMAL','avg', mean_NORMAL),
('RANDNORMAL','stdev', stdev_NORMAL),
('RANDNORMAL','skew', skew_NORMAL),
('RANDNORMAL','kurt', kurt_NORMAL),
('EXPECTED','avg',@mean),
('EXPECTED','stdev',@stdev),
('EXPECTED','skew',@skew),
('EXPECTED','kurt',@kurt)
)x(fn_name,stat,val_stat)
)d
PIVOT(sum(val_stat) FOR fn_name in([RANDNORMAL],[EXPECTED])) P

This produces the following result (your result will be different).

 stat RANDNORMAL EXPECTED avg 100.0010563 100 kurt -0.001653991 0 skew 0.004052033 0 stdev 15.00480078 15

### Support  Copyright 2008-2023 Westclintech LLC         Privacy Policy        Terms of Service