TRand


The TRand function creates a table (array) containing random numbers uniformly distributed over an interval. If an interval is not specified, the interval is [0,1].

Syntax

table = TRand( nCount )

table = TRand( nCount, nMaxInt )

table = TRand( nCount, nMin, nMax )

where

    nCount is the number of random numbers.

    nMaxInt is the maximum value and integer values are returned in the range [1, nMaxInt].

    nMin and nMax are the endpoints of the interval and real numbers are retrned in the range [nMin, nMax]. If nMin is specified, nMax also must be specified.

    table is the returned table containing nCount values.

  

This function creates a table of values. To obtain a single random value, use the Lua library function math.rand(). Note that the standard deviation of a uniformly distributed random sample is (nMax-nMin)/sqrt(12).

Since the same table is returned, this function may be used inline to modify the data For example, you might use TSort(TRand(t)) to create a table t of random deviates sorted into ascending order.

Example

The following script creates 3 uniform random deviates and plots them ordinally:

x = { 1, 2, 3 }

-- create a table with values 1, 2, 3

y = TRand(3)

-- create table of 3 random values

PlotScatter( x, y )

-- make the scatterplot of y against x

The following script does the same thing more compactly:

PlotScatter( TSeries(3), TRand(3) )

-- make the scatterplot

The following script plots n uniform random deviates from the interval [100,152]:

n = 1000

-- use 1000 values

PlotScatter( TSeries(n), TRand(n,100,152) )

-- make the scatterplot

Related Topics

TGaussDev

GaussDev

CStats class