Previous Up Next

7.3.8  Producing random matrices: randmatrix ranm randMat

You can produce a random vector or matrix with the randmatrix (or ranm or randMat) command. (See also sections 5.27.25 and 5.44.3.) The randmatrix command has the following possible arguments.

An integer n
With an integer n, randmatrix(n) will return a vector of length n whose elements are integers chosen randomly from [−99,−98,…,98,99] with equal probability. If you enter
randmatrix(5)
you might get
[86,-97,-82,7,-27]
Two integers n and p
Given two integers n and p, randmatrix(n,p) will return an n× p matrix whose elements are integers chosen randomly from [−99,99] with equal probability. If you enter
randmatrix(2,3)
you might get
[[26,-89,63],[-49,-86,-64]]
Three integers n, p and a
Given three integers n, p and a, randmatrix(n,p,a) will return an n× p matrix whose elements are integers chosen randomly from [0,a) (or (a,0] is a is negative) with equal probability. If you enter
randmatrix(2,3,10)
you might get
[[4,7,6],[7,4,5]]
Two integers n and p, and an interval a..b.
Given two integers n, p and an a..b, randmatrix(n,p,a..b) will return an n× p matrix whose elements are real numbers chosen randomly from [a,b) with equal probability. If you enter
randmatrix(2,3,0..1)
you might get
[[0.90923402831,0.594602484722,0.250897713937],[0.332611694932,0.145975249354,0.543010003399]]
Two integers n and p and a function (which must be quoted) to produce random numbers
In this case, the third argument must be one of ’rand(n)’, ’binomial(n,p)’, ’binomial,n,p, ’randbinomial(n,p)’, ’multinomial(P,K)’, ’multinomial,P,K, ’randmultinomial(P,K)’, ’poisson(λ)’, ’poisson, λ, ’randpoisson(λ)’, ’normald(µ,σ)’, ’normald,µ,σ, ’randnorm(µ,σ)’, ’exp(a)’, ’exp,a, ’randexp(a)’, ’fisher(n,m)’, ’fisher,n,m, or ’randfisher(n,m)’.

Given such an R, the command randmatrix(n,p,R) will return an n× p matrix whose elements are numbers chosen randomly according to the rule determined by R. If you enter

randmatrix(2,3,’randnorm(2,1)’)

you might get

[[2.6324726358,0.539273367446,0.793750476229],[2.24729803442,1.28189228187,2.25750809791]]

Previous Up Next