SQL Server RANDNORMAL Function
Updated 2023-10-18 16:12:50.517000
Description
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 [westclintech].[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 Type
table
| colName | colDatatype | colDesc |
|---|---|---|
| Seq | int | A monotonically increasing sequence number |
| X | float | The random variable |
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(*) as [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 |
|---|---|
| -5 | 1 |
| -4 | 30 |
| -3 | 1307 |
| -2 | 21510 |
| -1 | 135616 |
| 0 | 683067 |
| 1 | 135698 |
| 2 | 21408 |
| 3 | 1322 |
| 4 | 41 |
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 | 99.9969298163252 | 100 |
| kurt | -0.00220295058182929 | 0 |
| skew | 0.000405814354994069 | 0 |
| stdev | 15.0098425595394 | 15 |
See Also
NORMINV - Inverse of the normal distribution
NORMSINV - Inverse of the standard normal distribution
RANDBETA - Random numbers from a beta distribution
RANDBINOM - Random numbers from a binomial distribution
RANDCAUCHY - Random numbers from a Cauchy distribution
RANDCHISQ - Random numbers from a chi-squared distribution
RANDEXP - Random numbers from an exponential distribution
RANDFDIST - Random numbers from an F-distribution
RANDGAMMA - Random numbers from a gamma distribution
RANDLAPLACE - Random numbers from a LaPlace distribution
RANDLOGISTIC - Random numbers from a logistic distribution
RANDPOISSON - Random numbers from a Poisson distribution
RANDSNORMAL - Random numbers from the standard normal distribution