r = rand() r = rand(m1,m2,...) r = rand(m1,m2,...,key) r = rand(x) r = rand(x,key) s = rand("seed") rand("seed",s) rand(key) key = rand("info")
- m1, m2, ...
integers, the size of the random matrix
a string, the distribution of random numbers (default
key="uniform"). The possible values are:
a real or complex matrix. Only its real or complex content and its dimensions are taken into account.
a m1-by-m2-by-... real array of doubles with random entries.
a positive integer: the seed (default
The purpose of this function is to return a matrix of doubles with real or complex random entries. Depending on the input arguments, the function can return a random matrix of doubles, or can get or set the distribution of random numbers, or can get or set the seed of the random number generator.
Generate random numbers
returns a 1-by-1 matrix of doubles, with one random value.
is a random matrix with dimension
returns a random matrix with dimension
returns a random matrix with the same size as a. The matrix
ris real if
ais a real matrix and
ris complex if
ais a complex matrix.
Change the distribution of the random numbers
key input argument sets the distribution of the
generated random numbers.
- rand("uniform") or rand("u")
sets the generator to a uniform random number generator. Random numbers are uniformly distributed in the interval [0,1).
- rand("normal") or rand("n")
sets the generator to a normal (Gauss-Laplace) random number generator, with mean 0 and variance 1.
return the current distribution of the random generator ("uniform" or "normal")
Get or set the seed
It is possible to get or set the seed of the random number generator.
returns the current value of the seed.
sets the seed to
s=0at first call).
In the following example, we generate random doubles with various distributions.
// Get one random double (based on the current distribution) r=rand() // Get one 4-by-6 matrix of doubles (based on the current distribution) r=rand(4,6) // Get one 4-by-6 matrix of doubles with uniform entries r=rand(4,6,"uniform") // Produce a matrix of random doubles with the same size as x x=rand(4,4); r=rand(x,"normal") // Produce a 2-by-2-by-2 array of random doubles r=rand(2,2,2)
In the following example, we change the distribution of the
number generated by
We first produce normal numbers, then numbers uniform in [0,1).
// Set the rand generator to normal rand("normal") r=rand(4,6) // Get the current distribution key=rand("info") // Set the rand generator to uniform rand("uniform") r=rand(4,6) key=rand("info")
In the following example, we generate a 2-by-3 complex matrix of doubles, with normal distribution.
// Produce a 2-by-3 matrix of random complex doubles x=rand(2,3)+%i*rand(2,3) // Produce a matrix of random complex doubles with // normal entries and the same size as x r=rand(x,"normal")
In the following example, we plot the distribution of uniform
numbers produced by
r=rand(1000,1,"uniform"); scf(); histplot(10,r); xtitle("Uniform numbers from rand","X","Frequency")
In the following example, we plot the distribution of standard normal
numbers produced by
Get predictable or less predictable numbers
The "uniform" pseudo random number generator is a deterministic sequence which aims at reproducing a independent identically distributed numbers uniform in the interval [0,1).
In order to get reproducible simulations, the initial seed of the generator is
zero, such that the sequence will remain the same from a session to the other.
In other words, the first numbers produced by
rand() always are : 0.2113249, 0.7560439, ...
In some situations, we may want to initialize the seed of the generator in
order to produce less predictable sequences. In this case, we may initialize the
seed with the output of the
"uniform" random number generator is described in
"Urand, A Universal Random Number Generator" by
Michael A. Malcolm, Cleve B. Moler, Stan-Cs-73-334, January 1973, Computer
Science Department, School Of Humanities And Sciences, Stanford University.
It is a linear congruential generator of the form :
x = (a x + c) mod M
where the constants are
|a = 843314861|
|c = 453816693|
|M = 231|
According to the authors, this generator is a full length generator, that is to say, its period is M = 231 = 214748364.
"normal" random number generator is based on
the Box-Muller method, where source of the uniform random numbers is Urand.
The statistical quality of the generator
Better random number generators are available from
the grand function, in the
sense that they have both a larger period and better statistical properties.
In the case where the quality of the random numbers matters, we should
grand function instead.
grand function has more features.
|Report an issue|
|<< prbs_a||random||sample >>|