SQL Server MRAND Function
Updated 2023-10-20 15:01:52.987000
Description
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 [westclintech].[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 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 random numbers.
SELECT wct.MRAND(5, 5) as MRAND;
This produces the following result.
| MRAND |
|---|
| 0.771425607507781,0.134477493415809,0.10550751309167,0.84613314543205,0.798585127479669;0.62770994223082,0.333486620026402,0.80377489691776,0.916069948075372,0.872625331335061;0.380903457468796,0.921585860159987,0.12491698289519,0.343612862910895,0.240248040873673;0.345327849195026,0.946873843645153,0.340558091337121,0.684887842128467,0.50751098594978;0.751433997299259,0.288153123244715,0.41803163682019,0.574196451610977,0.140616484517519 |
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.MRAND(5, 5));
This produces the following result.
| RowNum | ColNum | ItemValue |
|---|---|---|
| 0 | 0 | 0.0917199370878376 |
| 0 | 1 | 0.412955388153417 |
| 0 | 2 | 0.483272484263066 |
| 0 | 3 | 0.71644585659562 |
| 0 | 4 | 0.531311061480693 |
| 1 | 0 | 0.705516660914531 |
| 1 | 1 | 0.765202311223933 |
| 1 | 2 | 0.511808382119894 |
| 1 | 3 | 0.138445003022647 |
| 1 | 4 | 0.135214033599577 |
| 2 | 0 | 0.94421862947951 |
| 2 | 1 | 0.126662550553057 |
| 2 | 2 | 0.44067387722464 |
| 2 | 3 | 0.854030737585402 |
| 2 | 4 | 0.84381542626946 |
| 3 | 0 | 0.56869187698173 |
| 3 | 1 | 0.366157882551736 |
| 3 | 2 | 0.217814114046196 |
| 3 | 3 | 0.977821422264828 |
| 3 | 4 | 0.645701881333115 |
| 4 | 0 | 0.595174356175202 |
| 4 | 1 | 0.712435110803896 |
| 4 | 2 | 0.660999368252698 |
| 4 | 3 | 0.783122740584948 |
| 4 | 4 | 0.519691777657574 |