SQL Server RANDCHISQ Function
Updated 2023-10-18 15:37:58.560000
Description
Use the table-valued function RANDCHISQ to generate a sequence of random numbers from a chi-squared distribution with @df degrees of freedom.
Syntax
SELECT * FROM [westclintech].[wct].[RANDCHISQ](
<@Rows, int,>
,<@df, 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.
@df
the degrees of freedom. @df 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
@df must be greater than zero.
If @df is NULL then @df is set to 1.
If @MaxIterations is less than 1 then no rows are returned.
Examples
In this example we create a sequence 1,000,000 random numbers rounded to one decimal place from a chi-squared distribution with @df = 1, COUNT the results, paste them into Excel and graph them.
SELECT X,
COUNT(*) as [COUNT]
FROM
(
SELECT ROUND(X, 1) as X
FROM wct.RANDCHISQ( 1000000, --@Rows
1 --@df
)
) n
GROUP BY X
ORDER BY X;
This produces the following result.

In this example we generate 1,000,000 random numbers from a chi-squared distribution with @df of 9. 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 @df as float = 9;
DECLARE @mean as float = @df;
DECLARE @var as float = 2*@df;
DECLARE @stdev as float = SQRT(@var);
DECLARE @skew as float = SQRT(8e+00/@df);
DECLARE @kurt as float = 12e+00/@df;
SELECT
stat,
[RANDCHISQ],
[EXPECTED]
FROM (
SELECT
x.*
FROM (
SELECT
AVG(x) as mean_CHISQ,
STDEVP(x) as stdev_CHISQ,
wct.SKEWNESS_P(x) as skew_CHISQ,
wct.KURTOSIS_P(x) as kurt_CHISQ
FROM
wct.RANDCHISQ(@size,@df)
)n
CROSS APPLY(
VALUES
('RANDCHISQ','avg', mean_CHISQ),
('RANDCHISQ','stdev', stdev_CHISQ),
('RANDCHISQ','skew', skew_CHISQ),
('RANDCHISQ','kurt', kurt_CHISQ),
('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([RANDCHISQ],[EXPECTED])) P;
This produces the following result (your result will be different).
| stat | RANDCHISQ | EXPECTED |
|---|---|---|
| avg | 9.00540686614531 | 9 |
| kurt | 1.36028654260851 | 1.33333333333333 |
| skew | 0.947320126675991 | 0.942809041582063 |
| stdev | 4.25133862650076 | 4.24264068711928 |
See Also
CHIINV - Calculate the inverse of the one-tailed probability of the chi-square distribution.
RANDBETA - Random numbers from a beta distribution
RANDBINOM - Random numbers from a binomial distribution
RANDCAUCHY - Random numbers from a Cauchy 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
RANDNORMAL - Random numbers from the normal distribution
RANDPOISSON - Random numbers from a Poisson distribution
RANDSNORMAL - Random numbers from the standard normal distribution