Generating random matrices

### 20.3.6 Generating random matrices

The randmatrix or ranm or randMat command produces random vectors and matrices. (See also Section 11.1.28 and Section 20.3.5.)

- randmatrix takes one mandatory argument and three optional arguments:
  - n, an integer.
  - Optionally, p, an integer.
  - Optionally, a, an integer.
  - Optionally, a..b, a range.
  - Optionally, distr, a distribution, which can be one of:
    - ’rand(n)’ (see Section 20.3.2).
    - ’binomial(n,p)’, ’binomial,n,p’ or ’randbinomial(n,p)’, for a binomial distribution (see Section 20.4.3 and Section 20.3.3).
    - ’multinomial(P,K)’, ’multinomial,P,K’ or ’randmultinomial(P,K)’ for a multinomial distribution (see Section 20.4.5 and Section 20.3.3).
    - ’poisson(λ)’, ’poisson, λ’ or ’randpoisson(λ)’ for a Poisson distribution (see Section 20.4.6 and Section 20.3.3).
    - ’normald(µ,σ)’, ’normald,µ,σ’ or ’randnorm(µ,σ)’ for a normal distribution (see Section 20.4.7 and Section 20.3.3).
    - ’exp(a)’, ’exp,a’ or ’randexp(a)’ for an exponential distribution (see Section 20.4.15 and Section 20.3.3).
    - ’fisher(n,m)’, ’fisher,n,m’ or ’randfisher(n,m)’ for a Fisher-Snédécor distribution (see Section 20.4.10 and Section 20.3.3).
    Note that distr is in quotes.
- randmatrix(n) returns a vector of length n whose elements are integers chosen randomly from {−99,−98,…,98,99} with equal probability.
- randmatrix(n,p) returns an n× p matrix whose elements are integers chosen randomly from {−99,…,99} with equal probability.
- randmatrix(n,p,a) returns an n× p matrix whose elements are integers chosen randomly from [0,a) (or (a,0] is a is negative) with equal probability.
- randmatrix(n,p,a..b) returns an n× p matrix whose elements are real numbers chosen randomly from [a,b) with equal probability.
- randmatrix(n,p,distr) returns an n× p matrix whose elements are numbers chosen randomly according to distribution distr.

#### Examples

randmatrix(5)

| ⎡ ⎣ | −48,54,28,−51,63 | ⎤ ⎦

randmatrix(2,3)

⎡ ⎢ ⎣ |
40 | −74 | −87
40 | −19 | 20
| ⎤ ⎥ ⎦

randmatrix(2,3,10)

⎡ ⎢ ⎣ |
4 | 2 | 1
4 | 4 | 0
| ⎤ ⎥ ⎦

randmatrix(2,3,0..1)

⎡ ⎢ ⎣ |
0.384355471935 | 0.655490326229 | 0.924850208685
0.159429819323 | 0.952957109548 | 0.220945354551
| ⎤ ⎥ ⎦

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

⎡ ⎢ ⎣ |
2.17670501195 | 0.653882567048 | 2.94543112983
2.46150672679 | 2.19251320854 | 2.44211638655
| ⎤ ⎥ ⎦