Please note that the recommended version of Scilab is 2024.0.0. This page might be outdated.
See the recommended documentation of this function
Random number generator(s)
Y=grand(m,n,'bet',A,B) Y=grand(m,n,'bin',N,p) Y=grand(m,n,'nbn',N,p) Y=grand(m,n,'chi', Df) Y=grand(m,n,'nch',Df,Xnon) Y=grand(m,n,'exp',Av) Y=grand(m,n,'f',Dfn,Dfd) Y=grand(m,n,'nf',Dfn,Dfd,Xnon) Y=grand(m,n,'gam',shape,scale) Y=grand(m,n,'nor',Av,Sd) Y=grand(n,'mn',Mean,Cov) Y=grand(m,n,'geom', p) Y=grand(n,'markov',P,x0) Y=grand(n,'mul',nb,P) Y=grand(m,n,'poi',mu) Y=grand(n,'prm',vect) Y=grand(m,n,'def') Y=grand(m,n,'unf',Low,High) Y=grand(m,n,'uin',Low,High) Y=grand(m,n,'lgi') S=grand('getgen') grand('setgen',gen) S=grand('getsd') grand('setsd',S) grand('setcgn',G) S=grand('getcgn') grand('initgn',I) grand('setall',s1,s2,s3,s4) grand('advnst',K)
- m, n
integers, size of the wanted matrix
a matrix whom only the dimensions (say
m x n) are used
a string given the distribution which (independants) variates are to be generated ('bin', 'nor', 'poi', etc ...)
- p1, ..., pk
the parameters (reals or integers) required to define completly the distribution
m x nrandom matrix
a string given the action onto the base generator(s) ('setgen' to change the current base generator, 'getgen' to retrieve the current base generator name, 'getsd' to retrieve the state (seeds) of the current base generator, etc ...)
- q1, ..., ql
the parameters (generally one string) needed to define the action
output of the action (generaly a string or a real column vector)
This function may be used to generate random numbers from various
distributions. In this case you must apply one of the
three first forms of the possible calling
sequences to get an
m x n matrix. The two
firsts are equivalent if
X is a
n matrix, and the third form corresponds to
'multivalued' distributions (e.g. multinomial, multivariate
gaussian, etc...) where a sample is a column vector (says of dim
m) and you get then
random vectors (as an
m x n matrix).
The last form is used to undertake various manipulations onto the base generators like changing the base generator (since v 2.7 you may choose between several base generators), changing or retrieving its internal state (seeds), etc ... These base generators give random integers following a uniform distribution on a large integer interval (lgi), all the others distributions being gotten from it (in general via a scheme lgi -> U([0,1)) -> wanted distribution).
Getting random numbers from a given distribution
Y=grand(m,n,'bet',A,B)generates random variates from the beta distribution with parameters
B. The density of the beta is (
0 < x < 1) :
A-1 B-1 x (1-x) / beta(A,B)
Bmust be reals >
10^(-37). Related function(s) : cdfbet.
Y=grand(m,n,'bin',N,p)generates random variates from the binomial distribution with parameters
N(positive integer) and
p(real in [0,1]) : number of successes in
Nindependant Bernouilli trials with probability
pof success. Related function(s) : binomial, cdfbin.
- negative binomial
Y=grand(m,n,'nbn',N,p)generates random variates from the negative binomial distribution with parameters
N(positive integer) and
p(real in (0,1)) : number of failures occurring before
Nsuccesses in independant Bernouilli trials with probability
pof success. Related function(s) : cdfnbn.
Y=grand(m,n,'chi', Df)generates random variates from the chisquare distribution with
Df(real > 0.0) degrees of freedom. Related function(s) : cdfchi.
- non central chisquare
Y=grand(m,n,'nch',Df,Xnon)generates random variates from the non central chisquare distribution with
Dfdegrees of freedom (real >= 1.0) and noncentrality parameter
Xnonc(real >= 0.0). Related function(s) : cdfchn.
Y=grand(m,n,'exp',Av)generates random variates from the exponential distribution with mean
Av(real >= 0.0).
- F variance ratio
Y=grand(m,n,'f',Dfn,Dfd)generates random variates from the F (variance ratio) distribution with
Dfn(real > 0.0) degrees of freedom in the numerator and
Dfd(real > 0.0) degrees of freedom in the denominator. Related function(s) : cdff.
- non central F variance ratio
Y=grand(m,n,'nf',Dfn,Dfd,Xnon)generates random variates from the noncentral F (variance ratio) distribution with
Dfn(real >= 1) degrees of freedom in the numerator, and
Dfd(real > 0) degrees of freedom in the denominator, and noncentrality parameter
Xnonc(real >= 0). Related function(s) : cdffnc.
Y=grand(m,n,'gam',shape,scale)generates random variates from the gamma distribution with parameters
shape(real > 0) and
scale(real > 0). The density of the gamma is :
shape (shape-1) -scale x scale x e / gamma(shape)
- Gauss Laplace (normal)
Y=grand(m,n,'nor',Av,Sd)generates random variates from the normal distribution with mean
Av(real) and standard deviation
Sd(real >= 0). Related function(s) : cdfnor.
- multivariate gaussian (multivariate normal)
nmultivariate normal random variates ;
Meanmust be a
m x 1matrix and
m x msymmetric positive definite matrix (
Yis then a
m x nmatrix).
Y=grand(m,n,'geom', p)generates random variates from the geometric distribution with parameter
p: number of Bernouilli trials (with probability succes of
p) until a succes is met.
pmust be in
pmin = 1.3 10^(-307)).
Ycontains positive real numbers with integer values, with are the "number of trials to get a success".
nsuccessive states of a Markov chain described by the transition matrix
P. Initial state is given by
x0is a matrix of size
Yis a matrix of size
m x n.
Y(i,:)is the sample path obtained from initial state
nobservations from the Multinomial distribution : class
P(i)is the probability that an event will be classified into category i.
Pthe vector of probabilities is of size
m-1(the probability of category
Yis of size
m x n, each column
Y(:,j)being an observation from multinomial distribution and
Y(i,j)the number of events falling in category
jth observation) (
sum(Y(:,j)) = nb).
Y=grand(m,n,'poi',mu)generates random variates from the Poisson distribution with mean
mu (real >= 0.0). Related function(s) : cdfpoi.
- random permutations
nrandom permutations of the column vector (
m x 1)
- uniform (def)
Y=grand(m,n,'def')generates random variates from the uniform distribution over
[0,1)(1 is never return).
- uniform (unf)
Y=grand(m,n,'unf',Low,High)generates random reals uniformly distributed in
- uniform (uin)
Y=grand(m,n,'uin',Low,High)generates random integers uniformly distributed between
Lowmust be integers such that
(High-Low+1) < 2,147,483,561.
- uniform (lgi)
Y=grand(m,n,'lgi')returns the basic output of the current generator : random integers following a uniform distribution over :
[0, 2^32 - 1]for mt, kiss and fsultra
[0, 2147483561]for clcg2
[0, 2^31 - 2]for clcg4
[0, 2^31 - 1]for urand.
Set/get the current generator and its state
The user has the possibility to choose between different base generators (which give random integers following the 'lgi' distribution, the others being gotten from it).
the Mersenne-Twister of M. Matsumoto and T. Nishimura, period about
2^19937, state given by an array of
624integers (plus an index onto this array); this is the default generator.
The Keep It Simple Stupid of G. Marsaglia, period about
2^123, state given by
a Combined 2 Linear Congruential Generator of P. L'Ecuyer, period about
2^61, state given by
2integers ; this was the only generator previously used by grand (but slightly modified).
a Combined 4 Linear Congruential Generator of P. L'Ecuyer, period about
2^121, state given by 4 integers ; this one is splitted in
101different virtual (non over-lapping) generators which may be useful for different tasks (see 'Actions specific to clcg4' and 'Test example for clcg4').
the generator used by the scilab function rand, state given by
1integer, period of
2^31(based on theory and suggestions given in d.e. knuth (1969), vol 2. State). This is the faster of this list but a little outdated (do not use it for serious simulations).
a Subtract-with-Borrow generator mixing with a congruential generator of Arif Zaman and George Marsaglia, period more than
10^356, state given by an array of 37 integers (plus an index onto this array, a flag (0 or 1) and another integer).
The differents actions common to all the generators, are:
- action= 'getgen'
S=grand('getgen')returns the current base generator (
Sis a string among 'mt', 'kiss', 'clcg2', 'clcg4', 'urand', 'fsultra'.
- action= 'setgen'
grand('setgen',gen)sets the current base generator to be
gena string among 'mt', 'kiss', 'clcg2', 'clcg4', 'urand', 'fsultra' (notes that this call returns the new current generator, ie gen).
- action= 'getsd'
S=grand('getsd')gets the current state (the current seeds) of the current base generator ;
Sis given as a column vector (of integers) of dimension
625for mt (the first being an index in
4for clcg4 (for this last one you get the current state of the current virtual generator) and
- action= 'setsd'
grand('setsd',S), grand('setsd',s1[,s2,s3,s4])sets the state of the current base generator (the new seeds) :
- for mt
Sis a vector of integers of dim
625(the first component is an index and must be in
624last ones must be in
[0,2^32[) (but must not be all zeros) ; a simpler initialisation may be done with only one integer
s1must be in
- for kiss
[0,2^32[must be provided ;
- for clcg2
[1,2147483398]must be given ;
- for clcg4
[1,2147483322]are required ;
CAUTION: with clcg4 you set the seeds of the current virtual generator but you may lost the synchronisation between this one and the others virtuals generators (ie the sequence generated is not warranty to be non over-lapping with a sequence generated by another virtual generator)=> use instead the 'setall' option.
- for urand
[0,2^31[ must be given.
- for fsultra
Sis a vector of integers of dim
40(the first component is an index and must be in
[0,37], the 2d component is a flag (0 or 1), the 3d an integer in [1,2^32[ and the 37 others integers in [0,2^32[) ; a simpler (and recommanded) initialisation may be done with two integers
- action= 'phr2sd'
Sd=grand('phr2sd', phrase)given a
phrase(character string) generates a
1 x 2vector
Sdwhich may be used as seeds to change the state of a base generator (initialy suited for clcg2).
Options specific to clcg4
The clcg4 generator may be used as the others generators but it offers the advantage
to be splitted in several (
101) virtual generators with non over-lapping
sequences (when you use a classic generator you may change the initial state (seeds)
in order to get another sequence but you are not warranty to get a complete different one).
Each virtual generator corresponds to a sequence of
2^72 values which is
further split into
V=2^31 segments (or blocks) of length
For a given virtual generator you have the possibility to return at the beginning of the
sequence or at the beginning of the current segment or to go directly at the next segment.
You may also change the initial state (seed) of the generator
0 with the
'setall' option which then change also the initial state of the other virtual generators
so as to get synchronisation (ie in function of the new initial state of gen
the initial state of gen
1..100 are recomputed so as to get
non over-lapping sequences.
- action= 'setcgn'
grand('setcgn',G)sets the current virtual generator for clcg4 (when clcg4 is set, this is the virtual (clcg4) generator number
Gwhich is used); the virtual clcg4 generators are numbered from
Gmust be an integer in
[0,100]) ; by default the current virtual generator is
- action= 'getcgn'
S=grand('getcgn')returns the number of the current virtual clcg4 generator.
- action= 'initgn'
grand('initgn',I)reinitializes the state of the current virtual generator
- I = -1
sets the state to its initial seed
- I = 0
sets the state to its last (previous) seed (i.e. to the beginning of the current segment)
- I = 1
sets the state to a new seed
Wvalues from its last seed (i.e. to the beginning of the next segment) and resets the current segment parameters.
- action= 'setall'
grand('setall',s1,s2,s3,s4)sets the initial state of generator
s1,s2,s3,s4. The initial seeds of the other generators are set accordingly to have synchronisation. For constraints on
s1, s2, s3, s4see the 'setsd' action.
- action= 'advnst'
grand('advnst',K)advances the state of the current generator by
2^Kvalues and resets the initial seed to that value.
Test example for clcg4
An example of the need of the splitting capabilities of clcg4 is as follows. Two statistical techniques are being compared on data of different sizes. The first technique uses bootstrapping and is thought to be as accurate using less data than the second method which employs only brute force. For the first method, a data set of size uniformly distributed between 25 and 50 will be generated. Then the data set of the specified size will be generated and analyzed. The second method will choose a data set size between 100 and 200, generate the data and analyze it. This process will be repeated 1000 times. For variance reduction, we want the random numbers used in the two methods to be the same for each of the 1000 comparisons. But method two will use more random numbers than method one and without this package, synchronization might be difficult. With clcg4, it is a snap. Use generator 0 to obtain the sample size for method one and generator 1 to obtain the data. Then reset the state to the beginning of the current block and do the same for the second method. This assures that the initial data for method two is that used by method one. When both have concluded, advance the block for both generators.
In the following example, we generate random numbers from various distributions and plot the associated histograms.
In the following example, we generate random numbers from the exponential distribution and then compare the empirical with the theoretical distribution.
In the following example, we generate random numbers from the gamma distribution and then compare the empirical with the theoretical distribution.
In the following example, we generate 10 random integers in the [1,365] interval.
In the following example, we generate 12 permutations of the [1,2,...,7] set. The 12 permutations are stored column-by-column.
This pseudo random number generator is a deterministic sequence which aims at reproducing a independent identically distributed numbers. In order to get reproducible simulations, the initial seed of the generator is constant, such that the sequence will remain the same from a session to the other. The first numbers produced by grand always are the same. In some situations, we may want to initialize the seed of the generator in order to produce less reproducible numbers. In this case, we may initialize the seed with the output of the getdate function:
The codes to generate sequences following other distributions than def, unf, lgi, uin and geom are from "Library of Fortran Routines for Random Number Generation", by Barry W. Brown and James Lovato, Department of Biomathematics, The University of Texas, Houston. The source code is available at : http://www.netlib.org/random/ranlib.f.tar.gz
The code is the mt19937int.c by M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator", ACM Trans. on Modeling and Computer Simulation Vol. 8, No. 1, January, pp.3-30 1998.
The code was given by G. Marsaglia at the end of a thread concerning RNG in C in several newsgroups (whom sci.math.num-analysis) "My offer of RNG's for C was an invitation to dance..." only kiss have been included in Scilab (kiss is made of a combinaison of severals others which are not visible at the scilab level).
The method is from P. L'Ecuyer but the C code is provided at Luc Devroye's home page (http://cg.scs.carleton.ca/~luc/rng.html See "lecuyer.c".). This generator is made of two linear congruential sequences s1 = a1*s1 mod m1, with a1 = 40014, m1 = 2147483563 and s2 = a2*s2 mod m2 , with a2 = 40692, m2 = 2147483399. The output is computed from output = s1-s2 mod (m1 - 1). Therefore, output is in [0, 2147483561]. The perido is about 2.3 10^18. The state is given by (s1, s2). In case of a user modification of the state we must have : s1 in [1, m1-1] and s2 in [1, m2-1]. The default initial seeds are s1 = 1234567890 and s2 = 123456789.
The code is from P. L'Ecuyer and Terry H.Andres and provided at the P. L'Ecuyer home page ( http://www.iro.umontreal.ca/~lecuyer/papers.html) A paper is also provided and this new package is the logical successor of an old 's one from : P. L'Ecuyer and S. Cote. Implementing a Random Number Package with Splitting Facilities. ACM Transactions on Mathematical Software 17:1,pp 98-111.
code from Arif Zaman (firstname.lastname@example.org) and George Marsaglia (email@example.com)
- scilab packaging
By Jean-Philippe Chancelier and Bruno Pincon
- This help page
Copyright (C) 2010 - DIGITEO - Michael Baudin
|<< Randlib||Randlib||ARnoldi PACKage >>|