SQL Server RANDSNORMAL Function
Updated 2023-10-18 19:33:29.050000
Description
Use the table-valued function RANDSNORMAL to generate a sequence of random numbers from the standard normal distribution.
Syntax
SELECT * FROM [westclintech].[wct].[RANDSNORMAL](
<@Rows, int,>)
Arguments
@Rows
the number of rows to generate. @Rows must be of the type int or of a type that implicitly converts to int.
Return Type
table
| colName | colDatatype | colDesc |
|---|---|---|
| Seq | int | A monotonically increasing sequence number |
| X | float | The random variable |
Remarks
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 the 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.RANDSNORMAL(1000000) ) n
GROUP BY X
ORDER BY 1;
This produces the following result. Your results will be different.
| X | COUNT |
|---|---|
| -4 | 32 |
| -3 | 1340 |
| -2 | 21481 |
| -1 | 135744 |
| 0 | 682726 |
| 1 | 136048 |
| 2 | 21333 |
| 3 | 1278 |
| 4 | 18 |
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
RANDNORMAL - Random numbers from the normal distribution
RANDPOISSON - Random numbers from a Poisson distribution