SQL Server MRANDN Function
Updated 2023-10-20 15:03:47.133000
Description
Use the scalar function MRANDN to generate an m-by-n matrix of pseudo-random numbers from the standard normal distribution.
Syntax
SELECT [westclintech].[wct].[MRANDN](
<@m, int,>
,<@n, int,>)
Arguments
@m
The number of rows in the matrix.
@n
The number of columns in the matrix.
Return Type
nvarchar(max)
Remarks
@m must be greater than or equal to 1.
@n must be greater than or equal to 1.
Examples
The following statement will produce the 5-by-5 matrix of pseudo-random numbers from the standard normal distribution.
SELECT wct.MRANDN(5, 5) as MRANDN;
This produces the following result.
| MRANDN |
|---|
| 0.409996709727112,-0.00447533995227935,-0.569381336855175,-0.444344964509572,-0.392413392483098;0.447740037998686,1.82518912506013,1.97445852461499,1.11767069519037,-0.548575208347825;-1.00418675832274,0.243530252013792,-0.632482682458317,1.47321822469948,0.196537106300479;0.403285434343591,1.69772959633896,0.153027620710726,0.74438203750852,1.56098232233076;0.533510355172554,0.0764553206281185,-1.65467638890231,0.111361220136488,-1.1633185022047 |
Since this is a matrix of random numbers, your results will be different.
We can use the table-valued function MATRIX to produce the output in third-normal form.
SELECT *
FROM wct.MATRIX(wct.MRANDN(5,5));
This produces the following result.
| RowNum | ColNum | ItemValue |
|---|---|---|
| 0 | 0 | -0.182917665796923 |
| 0 | 1 | -0.307999198184022 |
| 0 | 2 | -0.307359629916836 |
| 0 | 3 | 0.680657933114575 |
| 0 | 4 | 0.524831239703179 |
| 1 | 0 | -0.853272587740419 |
| 1 | 1 | -0.416493322946868 |
| 1 | 2 | -1.16459866817653 |
| 1 | 3 | -1.2475033746981 |
| 1 | 4 | -2.20881434337242 |
| 2 | 0 | 1.05329158720774 |
| 2 | 1 | 0.866228021008215 |
| 2 | 2 | 0.245181973863511 |
| 2 | 3 | -0.115983115116491 |
| 2 | 4 | -0.0635604193448651 |
| 3 | 0 | 1.16601372633918 |
| 3 | 1 | 1.97154575890409 |
| 3 | 2 | -0.591647363421029 |
| 3 | 3 | -0.81158959859554 |
| 3 | 4 | 0.309715756989705 |
| 4 | 0 | 0.565948667517376 |
| 4 | 1 | 0.571141049105415 |
| 4 | 2 | -2.11372801052463 |
| 4 | 3 | -1.59100488073806 |
| 4 | 4 | 0.541094492166664 |