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].
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.
The following script creates 3 uniform random deviates and plots them ordinally:
|
-- create a table with values 1, 2, 3 |
|
-- create table of 3 random values |
|
-- make the scatterplot of y against x |
The following script does the same thing more compactly:
|
-- make the scatterplot |
The following script plots n uniform random deviates from the interval [100,152]:
|
-- use 1000 values |
|
-- make the scatterplot |