# SQL Server matrix of pseudo-random numbers

MRAND

Updated: 31 January 2012

Use the scalar function MRAND to generate an m-by-n matrix of pseudo-random numbers greater than or equal to zero and less than one.
Syntax
SELECT [wctMath].[wct].[MRAND](
<@m, int,>
,<@n, int,>)
Arguments
@m
The number of rows in the random number matrix.
@n
The number of columns in the random number matrix.
Return Types
[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 random numbers.
SELECT wct.MRAND(5,5) as MRAND
This produces the following result.
MRAND
-----------------------------------------------------------------------------
0.330019295363187,0.406989189713348,0.590313154064414,0.587542254659555,
0.142613400401338;0.376963616844595,0.342615205819812,0.664582753507344,
0.256029382341586,0.825038029482458;0.669611947235748,0.38944349521547,
0.779861850965505,0.324138246929596,0.619999822364317;0.184216305512321,
0.368234595541736,0.0189107717512636,0.371563031221086,0.367610267138681;
0.931777569716552,0.0965590670946356,0.732310123678345,0.462206366031522,
0.0543666673741438

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((SELECT wct.MRAND(5,5)))

This produces the following result.
RowNum      ColNum              ItemValue
----------- ----------- ----------------------
0           0      0.358830339276845
0           1      0.831555428762548
0           2      0.428364833385385
0           3      0.848638295509437
0           4      0.695542866230018
1           0      0.423286971066211
1           1      0.806047911839642
1           2      0.663980873704239
1           3      0.661668902136982
1           4      0.181803888940008
2           0      0.361497946157799
2           1      0.240072252253109
2           2        0.6655069966586
2           3      0.781934735723197
2           4      0.712597877583602
3           0      0.261741738449994
3           1      0.419792679404657
3           2      0.218134731411943
3           3      0.183349333578691
3           4      0.255867348507332
4           0      0.142887803429557
4           1       0.54777726720222
4           2      0.471058376865614
4           3      0.283647425287708
4           4      0.285713850857508