Logo

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.

RowNumColNumItemValue
000.0917199370878376
010.412955388153417
020.483272484263066
030.71644585659562
040.531311061480693
100.705516660914531
110.765202311223933
120.511808382119894
130.138445003022647
140.135214033599577
200.94421862947951
210.126662550553057
220.44067387722464
230.854030737585402
240.84381542626946
300.56869187698173
310.366157882551736
320.217814114046196
330.977821422264828
340.645701881333115
400.595174356175202
410.712435110803896
420.660999368252698
430.783122740584948
440.519691777657574