11
11
* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" version="5.0-subset Scilab" xml:lang="en" xml:id="grand">
16
<refname>grand</refname>
17
<refpurpose>Random numbers</refpurpose>
20
<title>Calling Sequence</title>
22
Y=grand(m,n,"bet",A,B)
23
Y=grand(m,n,"bin",N,p)
24
Y=grand(m,n,"nbn",N,p)
25
Y=grand(m,n,"chi", Df)
26
Y=grand(m,n,"nch",Df,Xnon)
28
Y=grand(m,n,"f",Dfn,Dfd)
29
Y=grand(m,n,"nf",Dfn,Dfd,Xnon)
30
Y=grand(m,n,"gam",shape,scale)
31
Y=grand(m,n,"nor",Av,Sd)
32
Y=grand(m,n,"geom", p)
35
Y=grand(m,n,"unf",Low,High)
36
Y=grand(m,n,"uin",Low,High)
41
Y=grand(n,"mn",Mean,Cov)
42
Y=grand(n,"markov",P,x0)
52
S=grand("phr2sd",phrase)
59
grand("setall",s1,s2,s3,s4)
65
<title>Arguments</title>
71
integers, size of the wanted matrix <literal>Y</literal>
79
a matrix whom only the dimensions (say <literal>m</literal>-by-<literal>n</literal>)
88
a <literal>m</literal>-by-<literal>n</literal> matrix of doubles, with random entries
95
<para>output of the action (a string or a real column vector)</para>
101
<title>Description</title>
103
This function generates random numbers from various
107
The calling sequences:
110
Y=grand(m,n,"bet",A,B)
111
Y=grand(m,n,"bin",N,p)
112
Y=grand(m,n,"nbn",N,p)
113
Y=grand(m,n,"chi", Df)
114
Y=grand(m,n,"nch",Df,Xnon)
115
Y=grand(m,n,"exp",Av)
116
Y=grand(m,n,"f",Dfn,Dfd)
117
Y=grand(m,n,"nf",Dfn,Dfd,Xnon)
118
Y=grand(m,n,"gam",shape,scale)
119
Y=grand(m,n,"nor",Av,Sd)
120
Y=grand(m,n,"geom", p)
121
Y=grand(m,n,"poi",mu)
123
Y=grand(m,n,"unf",Low,High)
124
Y=grand(m,n,"uin",Low,High)
128
produce a <literal>m</literal>-by-<literal>n</literal> matrix
132
The calling sequence:
138
where <literal>X</literal> is a <literal>m</literal>-by-<literal>n</literal> matrix,
139
produce the same effect.
140
In this case, only the size of <literal>X</literal> is used.
143
The calling sequences:
146
Y=grand(n,"mn",Mean,Cov)
147
Y=grand(n,"markov",P,x0)
148
Y=grand(n,"mul",nb,P)
149
Y=grand(n,"prm",vect)
152
produce a <literal>m</literal>-by-<literal>n</literal> matrix
153
with random entries, where <literal>m</literal> is the size of the argument <literal>Mean</literal>,
154
<literal>Cov</literal>, <literal>P</literal> or <literal>vect</literal> depending
155
on the case (see below for details).
158
The calling sequences:
172
grand("setall",s1,s2,s3,s4)
177
configure or quiery the state of the underlying random number
182
<title>Getting random numbers from a given distribution</title>
188
<literal>Y=grand(m,n,"bet",A,B)</literal> generates
189
random variates from the beta distribution with parameters
190
<literal>A</literal> and <literal>B</literal>. The density
191
of the beta distribution is <latex><![CDATA[(0 < x < 1)]]></latex>:
195
\dfrac{x^{A-1}(1-x)^{B-1}}{\beta(A,B)}
199
<literal>A</literal> and <literal>B</literal> must be
200
reals <latex><![CDATA[>10^{-37}]]></latex>. Related function: <link linkend="cdfbet">cdfbet</link>.
205
<term>binomial</term>
208
<literal>Y=grand(m,n,"bin",N,p)</literal> generates random
209
variates from the binomial distribution with parameters
210
<literal>N</literal> (positive integer) and <literal>p</literal>
211
(real in [0,1]) : number of successes in <literal>N</literal>
212
independent Bernouilli trials with probability <literal>p</literal>
213
of success. Related functions: <link linkend="binomial">binomial</link>,
214
<link linkend="cdfbin">cdfbin</link>.
219
<term>negative binomial</term>
222
<literal>Y=grand(m,n,"nbn",N,p)</literal> generates random variates from the
223
negative binomial distribution with parameters <literal>N</literal> (positive integer) and <literal>p</literal> (real
224
in (0,1)) : number of failures occurring before <literal>N</literal> successes
225
in independent Bernouilli trials with probability <literal>p</literal> of success.
226
Related function: <link linkend="cdfnbn">cdfnbn</link>.
231
<term>chisquare</term>
234
<literal>Y=grand(m,n,"chi", Df)</literal> generates random
235
variates from the chi-square distribution with <literal>Df</literal>
236
(real > 0.0) degrees of freedom. Related function:
237
<link linkend="cdfchi">cdfchi</link>.
242
<term>non-central chi-square</term>
245
<literal>Y=grand(m,n,"nch",Df,Xnonc)</literal> generates
246
random variates from the non-central chisquare
247
distribution with <literal>Df</literal> degrees of freedom
248
(real >= 1.0) and noncentrality parameter
249
<literal>Xnonc</literal> (real >= 0.0). Related
250
function: <link linkend="cdfchn">cdfchn</link>.
255
<term>exponential</term>
258
<literal>Y=grand(m,n,"exp",Av)</literal> generates
259
random variates from the exponential distribution with mean
260
<literal>Av</literal> (real > 0.0).
265
<term>F variance ratio</term>
268
<literal>Y=grand(m,n,"f",Dfn,Dfd)</literal> generates
269
random variates from the F (variance ratio) distribution
270
with <literal>Dfn</literal> (real > 0.0) degrees of
271
freedom in the numerator and <literal>Dfd</literal> (real
272
> 0.0) degrees of freedom in the denominator. Related
273
function : <link linkend="cdff">cdff</link>.
278
<term>non-central F variance ratio</term>
281
<literal>Y=grand(m,n,"nf",Dfn,Dfd,Xnonc)</literal>
282
generates random variates from the noncentral F (variance
283
ratio) distribution with <literal>Dfn</literal> (real
284
>= 1) degrees of freedom in the numerator, and
285
<literal>Dfd</literal> (real > 0) degrees of freedom in
286
the denominator, and noncentrality parameter
287
<literal>Xnonc</literal> (real >= 0). Related
288
function : <link linkend="cdffnc">cdffnc</link>.
296
<literal>Y=grand(m,n,"gam",shape,scale)</literal>
297
generates random variates from the gamma distribution with
298
parameters <literal>shape</literal> (real > 0) and
299
<literal>scale</literal> (real > 0). The density of the
300
gamma distribution is :
304
\dfrac{ \textrm{scale}^{\textrm{shape}} x^{\textrm{shape}-1} e^{-\textrm{scale} x}}{\gamma(\textrm{shape}) }
308
Related functions : <link linkend="gamma">gamma</link>,
309
<link linkend="cdfgam">cdfgam</link>.
314
<term>Gauss Laplace (normal)</term>
317
<literal>Y=grand(m,n,"nor",Av,Sd)</literal> generates
318
random variates from the normal distribution with mean
319
<literal>Av</literal> (real) and standard deviation
320
<literal>Sd</literal> (real >= 0). Related function
321
: <link linkend="cdfnor">cdfnor</link>.
326
<term>multivariate gaussian (multivariate normal)</term>
329
<literal>Y=grand(n,"mn",Mean,Cov)</literal>
330
generates multivariate normal random
331
variates; <literal>Mean</literal> must be a <literal>m x 1</literal>
332
column vector and <literal>Cov</literal> a <literal>m</literal>-by-<literal>m</literal>
333
symmetric positive definite matrix (<literal>Y</literal> is then
334
a <literal>m</literal>-by-<literal>n</literal> matrix).
339
<term>geometric</term>
342
<literal>Y=grand(m,n,"geom", p)</literal> generates
343
random variates from the geometric distribution with
344
parameter <literal>p</literal> : number of Bernouilli trials
345
(with probability succes of <literal>p</literal>) until a
346
succes is met. <literal>p</literal> must be in
347
<latex>[p_{min},1]</latex> (with <latex> p_{min} = 1{,}3\times 10^{-307} </latex>).
350
<literal>Y</literal> contains positive real numbers
351
with integer values, whiсh are the "number of trials to get
360
<literal>Y=grand(n,"markov",P,x0)</literal> generate
361
<literal>n</literal> successive states of a Markov chain
362
described by the transition matrix
363
<literal>P</literal>. A sum of each the rows in <literal>P</literal> is 1. Initial state is given by <literal>x0</literal>. If <literal>x0</literal> is a
364
matrix of size <literal>m=size(x0,"*")</literal> then
365
<literal>Y</literal> is a matrix of size <literal>m</literal>-by-<literal>n</literal>. <literal>Y(i,:)</literal> is the sample path
366
obtained from initial state <literal>x0(i)</literal>.
371
<term>multinomial</term>
374
<literal>Y=grand(n,"mul",nb,P)</literal> generates
375
<literal>n</literal> observations from the Multinomial
376
distribution : class <literal>nb</literal> events in
377
<literal>m</literal> categories (put <literal>nb</literal>
378
"balls" in <literal>m</literal>
379
"boxes"). <literal>P(i)</literal> is the probability that
380
an event will be classified into category
381
<literal>i</literal>. <literal>P</literal> the vector of probabilities is of
382
size <literal>m-1</literal> (the probability of category
383
<literal>m</literal> is <literal>1-sum(P)</literal>).
384
<literal>Y</literal> is of size <literal>m</literal>-by-<literal>n</literal>.
385
Each column <literal>Y(:,j)</literal> is an observation
386
from multinomial distribution and
387
<literal>Y(i,j)</literal> is the number of events falling in
388
category <literal>i</literal> (for the
389
<literal>j</literal>th observation) (<literal>sum(Y(:,j)) = nb</literal>).
397
<literal>Y=grand(m,n,"poi",mu)</literal> generates
398
random variates from the Poisson distribution with mean
399
<literal>mu</literal> (real >= 0.0). Related function
400
: <link linkend="cdfpoi">cdfpoi</link>.
405
<term>random permutations</term>
408
<literal>Y=grand(n,"prm",vect)</literal> generate
409
<literal>n</literal> random permutations of the column
410
vector (<literal>m x 1</literal>)
411
<literal>vect</literal>.
416
<term>uniform (def)</term>
419
<literal>Y=grand(m,n,"def")</literal> generates
420
random variates from the uniform distribution over
421
<literal>[0,1)</literal> (1 is never return).
426
<term>uniform (unf)</term>
429
<literal>Y=grand(m,n,"unf",Low,High)</literal>
430
generates random reals uniformly distributed in
431
<literal>[Low, High)</literal> (<literal>High</literal> is never return).
436
<term>uniform (uin)</term>
439
<literal>Y=grand(m,n,"uin",Low,High)</literal>
440
generates random integers uniformly distributed between
441
<literal>Low</literal> and <literal>High</literal>
442
(included). <literal>High</literal> and
443
<literal>Low</literal> must be integers such that
444
<literal>(High-Low+1) < 2,147,483,561</literal>.
449
<term>uniform (lgi)</term>
452
<literal>Y=grand(m,n,"lgi")</literal> returns the
453
basic output of the current generator : random integers
454
following a uniform distribution over :
459
<literal>[0, 2^32 - 1]</literal> for mt, kiss and fsultra;
464
<literal>[0, 2147483561]</literal> for clcg2;
469
<literal>[0, 2^31 - 2]</literal> for clcg4;
474
<literal>[0, 2^31 - 1]</literal> for urand.
483
<title>Set/get the current generator and its state</title>
485
The user has the possibility to choose between different base
486
generators (which give random integers following the "lgi" distribution, the others
487
being gotten from it).
494
The Mersenne-Twister of M. Matsumoto and T. Nishimura, period about <literal>2^19937</literal>,
495
state given by an array of <literal>624</literal> integers (plus an index onto this array); this
496
is the default generator.
504
The "Keep It Simple Stupid" of G. Marsaglia, period about <literal>2^123</literal>,
505
state given by <literal>4</literal> integers.
513
A Combined 2 Linear Congruential Generator of P. L'Ecuyer,
514
period about <literal>2^61</literal>, state given by <literal>2</literal> integers.
522
A Combined 4 Linear Congruential Generator of P. L'Ecuyer,
523
period about <literal>2^121</literal>, state given by 4 integers ; this one is
524
split in <literal>101</literal> different virtual (non-overlapping) generators
525
which may be useful for different tasks (see "Actions specific to clcg4" and
526
"Test example for clcg4").
534
A Subtract-with-Borrow generator mixing with a congruential
535
generator of Arif Zaman and George Marsaglia, period more than <literal>10^356</literal>,
536
state given by an array of 37 integers (plus an index onto this array, a flag (0 or 1)
537
and another integer).
545
The generator used by the scilab function <link linkend="rand">rand</link>, state
546
given by <literal>1</literal> integer, period of <literal>2^31</literal>.
547
This generator is based on "Urand, A Universal Random Number Generator" By,
548
Michael A. Malcolm, Cleve B. Moler, Stan-Cs-73-334, January 1973, Computer
549
Science Department, School Of Humanities And Sciences, Stanford University.
550
This is the faster of this list but its statistical qualities are less
551
satisfactory than the other generators.
557
The differents actions common to all the generators, are:
561
<term>action= "getgen"</term>
564
<literal>S=grand("getgen")</literal> returns the current base generator.
565
In this case <literal>S</literal> is
566
a string among "mt", "kiss", "clcg2", "clcg4", "urand", "fsultra".
571
<term>action= "setgen"</term>
574
<literal>grand("setgen",gen)</literal> sets the current base generator to be <literal>gen</literal>
575
a string among "mt", "kiss", "clcg2", "clcg4", "urand", "fsultra".
576
Notice that this call returns the new current generator, i.e. <literal>gen</literal>.
581
<term>action= "getsd"</term>
584
<literal>S=grand("getsd")</literal> gets the current state (the current seeds) of the current base
585
generator ; <literal>S</literal> is given as a column vector (of integers) of dimension <literal>625</literal>
586
for mt (the first being an index in <literal>[1,624]</literal>), <literal>4</literal> for kiss, <literal>2</literal>
587
for clcg2, <literal>40</literal> for fsultra, <literal>4</literal> for clcg4
588
(for this last one you get the current state of the current virtual generator) and <literal>1</literal>
594
<term>action= "setsd"</term>
597
<literal>grand("setsd",S), grand("setsd",s1[,s2,s3,s4])</literal> sets the state of the current
598
base generator (the new seeds) :
605
<literal>S</literal> is a vector of integers of dim <literal>625</literal> (the first component is an index
606
and must be in <literal>[1,624]</literal>, the <literal>624</literal> last ones must be in
607
<literal>[0,2^32[</literal>) (but must not be all zeros) ; a simpler initialisation may be done
608
with only one integer <literal>s1</literal> (<literal>s1</literal> must be in <literal>[0,2^32[</literal>) ;
613
<term>for kiss</term>
616
<literal>4</literal> integers <literal>s1,s2, s3,s4</literal> in <literal>[0,2^32[</literal> must be provided ;
621
<term>for clcg2</term>
624
<literal>2</literal> integers <literal>s1</literal> in <literal>[1,2147483562]</literal> and <literal>s2</literal>
625
in <literal>[1,2147483398]</literal> must be given ;
630
<term>for clcg4</term>
633
<literal>4</literal> integers <literal>s1</literal> in <literal>[1,2147483646]</literal>, <literal>s2</literal>
634
in <literal>[1,2147483542]</literal>, <literal>s3</literal> in <literal>[1,2147483422]</literal>,
635
<literal>s4</literal> in <literal>[1,2147483322]</literal> are required ;
638
<literal>CAUTION</literal> : with clcg4 you set the seeds of the current virtual
639
generator but you may lost the synchronisation between this one
640
and the others virtuals generators (i.e. the sequence generated
641
is not warranty to be non-overlapping with a sequence generated
642
by another virtual generator)=> use instead the "setall" option.
647
<term>for urand</term>
650
<literal>1</literal> integer <literal>s1</literal> in <literal>[0,2^31[</literal> must be given.
655
<term>for fsultra</term>
658
<literal>S</literal> is a vector of integers of dim <literal>40</literal> (the first component
659
is an index and must be in <literal>[0,37]</literal>, the 2nd component is a flag (0 or 1), the 3rd component is
660
an integer in <literal>[1,2^32[</literal> and the 37 others integers are in <literal>[0,2^32[</literal>) ; a simpler (and recommanded)
661
initialisation may be done with two integers <literal>s1</literal> and <literal>s2</literal> in
662
<literal>[0,2^32[</literal>.
670
<term>action= "phr2sd"</term>
673
<literal>Sd=grand("phr2sd", phrase)</literal> given a <literal>phrase</literal> (character string) generates
674
a <literal>1 x 2</literal> vector <literal>Sd</literal> which may be used as seeds to change the state of a
675
base generator (initialy suited for clcg2).
682
<title>Options specific to clcg4</title>
684
The clcg4 generator may be used as the others generators but it offers the advantage
685
to be split in several (<literal>101</literal>) virtual generators with non-overlapping
686
sequences (when you use a classic generator you may change the initial state (seeds)
687
in order to get another sequence but you are not warranty to get a complete different one).
688
Each virtual generator corresponds to a sequence of <literal>2^72</literal> values which is
689
further split into <literal>V=2^31</literal> segments (or blocks) of length <literal>W=2^41</literal>.
690
For a given virtual generator you have the possibility to return at the beginning of the
691
sequence or at the beginning of the current segment or to go directly at the next segment.
692
You may also change the initial state (seed) of the generator <literal>0</literal> with the
693
"setall" option which then change also the initial state of the other virtual generators
694
so as to get synchronisation, i.e. in function of the new initial state of gen <literal>0</literal>
695
the initial state of gen <literal>1..100</literal> are recomputed so as to get <literal>101</literal>
696
non-overlapping sequences.
700
<term>action= "setcgn"</term>
703
<literal>grand("setcgn",G)</literal> sets the current virtual generator for clcg4 (when clcg4
704
is set, this is the virtual (clcg4) generator number <literal>G</literal> which is used); the virtual clcg4
705
generators are numbered from <literal>0,1,...,100</literal> (and so <literal>G</literal> must be an integer
706
in <literal>[0,100]</literal>) ; by default the current virtual generator is <literal>0</literal>.
711
<term>action= "getcgn"</term>
714
<literal>S=grand("getcgn")</literal> returns the number of the current virtual clcg4 generator.
719
<term>action= "initgn"</term>
722
<literal>grand("initgn",I)</literal> reinitializes the state of the current virtual generator
728
<para>sets the state to its initial seed</para>
734
<para>sets the state to its last (previous) seed (i.e. to the beginning of the current segment)</para>
741
sets the state to a new seed <literal>W</literal> values from its last seed (i.e. to the beginning
742
of the next segment) and resets the current segment parameters.
750
<term>action= "setall"</term>
753
<literal>grand("setall",s1,s2,s3,s4)</literal> sets the initial state of generator <literal>0</literal>
754
to <literal>s1,s2,s3,s4</literal>. The initial seeds of the other generators are set accordingly
755
to have synchronisation. For constraints on <literal>s1, s2, s3, s4</literal> see the "setsd" action.
760
<term>action= "advnst"</term>
763
<literal>grand("advnst",K)</literal> advances the state of the current generator by <literal>2^K</literal> values
764
and resets the initial seed to that value.
771
<title>Examples</title>
773
In the following example, we generate random numbers from various distributions and
774
plot the associated histograms.
776
<programlisting role="example"><![CDATA[
14
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" version="5.0-subset Scilab" xml:lang="en" xml:id="grand">
16
<refname>grand</refname>
17
<refpurpose>Random numbers</refpurpose>
20
<title>Calling Sequence</title>
22
Y=grand(m,n,"bet",A,B)
23
Y=grand(m,n,"bin",N,p)
24
Y=grand(m,n,"nbn",N,p)
25
Y=grand(m,n,"chi", Df)
26
Y=grand(m,n,"nch",Df,Xnon)
28
Y=grand(m,n,"f",Dfn,Dfd)
29
Y=grand(m,n,"nf",Dfn,Dfd,Xnon)
30
Y=grand(m,n,"gam",shape,scale)
31
Y=grand(m,n,"nor",Av,Sd)
32
Y=grand(m,n,"geom", p)
35
Y=grand(m,n,"unf",Low,High)
36
Y=grand(m,n,"uin",Low,High)
41
Y=grand(n,"mn",Mean,Cov)
42
Y=grand(n,"markov",P,x0)
52
S=grand("phr2sd",phrase)
59
grand("setall",s1,s2,s3,s4)
65
<title>Arguments</title>
71
integers, size of the wanted matrix <literal>Y</literal>
79
a matrix whom only the dimensions (say <literal>m</literal>-by-<literal>n</literal>)
88
a <literal>m</literal>-by-<literal>n</literal> matrix of doubles, with random entries
95
<para>output of the action (a string or a real column vector)</para>
101
<title>Description</title>
103
This function generates random numbers from various
107
The calling sequences:
110
Y=grand(m,n,"bet",A,B)
111
Y=grand(m,n,"bin",N,p)
112
Y=grand(m,n,"nbn",N,p)
113
Y=grand(m,n,"chi", Df)
114
Y=grand(m,n,"nch",Df,Xnon)
115
Y=grand(m,n,"exp",Av)
116
Y=grand(m,n,"f",Dfn,Dfd)
117
Y=grand(m,n,"nf",Dfn,Dfd,Xnon)
118
Y=grand(m,n,"gam",shape,scale)
119
Y=grand(m,n,"nor",Av,Sd)
120
Y=grand(m,n,"geom", p)
121
Y=grand(m,n,"poi",mu)
123
Y=grand(m,n,"unf",Low,High)
124
Y=grand(m,n,"uin",Low,High)
128
produce a <literal>m</literal>-by-<literal>n</literal> matrix
132
The calling sequence:
138
where <literal>X</literal> is a <literal>m</literal>-by-<literal>n</literal> matrix,
139
produce the same effect.
140
In this case, only the size of <literal>X</literal> is used.
143
The calling sequences:
146
Y=grand(n,"mn",Mean,Cov)
147
Y=grand(n,"markov",P,x0)
148
Y=grand(n,"mul",nb,P)
149
Y=grand(n,"prm",vect)
152
produce a <literal>m</literal>-by-<literal>n</literal> matrix
153
with random entries, where <literal>m</literal> is the size of the argument <literal>Mean</literal>,
154
<literal>Cov</literal>, <literal>P</literal> or <literal>vect</literal> depending
155
on the case (see below for details).
158
The calling sequences:
172
grand("setall",s1,s2,s3,s4)
177
configure or quiery the state of the underlying random number
182
<title>Getting random numbers from a given distribution</title>
188
<literal>Y=grand(m,n,"bet",A,B)</literal> generates
189
random variates from the beta distribution with parameters
190
<literal>A</literal> and <literal>B</literal>. The density
191
of the beta distribution is <latex><![CDATA[(0 < x < 1)]]></latex>:
195
\dfrac{x^{A-1}(1-x)^{B-1}}{\beta(A,B)}
199
<literal>A</literal> and <literal>B</literal> must be
200
reals <latex><![CDATA[>10^{-37}]]></latex>. Related function: <link linkend="cdfbet">cdfbet</link>.
205
<term>binomial</term>
208
<literal>Y=grand(m,n,"bin",N,p)</literal> generates random
209
variates from the binomial distribution with parameters
210
<literal>N</literal> (positive integer) and <literal>p</literal>
211
(real in [0,1]) : number of successes in <literal>N</literal>
212
independent Bernouilli trials with probability <literal>p</literal>
213
of success. Related functions: <link linkend="binomial">binomial</link>,
214
<link linkend="cdfbin">cdfbin</link>.
219
<term>negative binomial</term>
222
<literal>Y=grand(m,n,"nbn",N,p)</literal> generates random variates from the
223
negative binomial distribution with parameters <literal>N</literal> (positive integer) and <literal>p</literal> (real
224
in (0,1)) : number of failures occurring before <literal>N</literal> successes
225
in independent Bernouilli trials with probability <literal>p</literal> of success.
226
Related function: <link linkend="cdfnbn">cdfnbn</link>.
231
<term>chisquare</term>
234
<literal>Y=grand(m,n,"chi", Df)</literal> generates random
235
variates from the chi-square distribution with <literal>Df</literal>
236
(real > 0.0) degrees of freedom. Related function:
237
<link linkend="cdfchi">cdfchi</link>.
242
<term>non-central chi-square</term>
245
<literal>Y=grand(m,n,"nch",Df,Xnonc)</literal> generates
246
random variates from the non-central chisquare
247
distribution with <literal>Df</literal> degrees of freedom
248
(real >= 1.0) and noncentrality parameter
249
<literal>Xnonc</literal> (real >= 0.0). Related
250
function: <link linkend="cdfchn">cdfchn</link>.
255
<term>exponential</term>
258
<literal>Y=grand(m,n,"exp",Av)</literal> generates
259
random variates from the exponential distribution with mean
260
<literal>Av</literal> (real > 0.0).
265
<term>F variance ratio</term>
268
<literal>Y=grand(m,n,"f",Dfn,Dfd)</literal> generates
269
random variates from the F (variance ratio) distribution
270
with <literal>Dfn</literal> (real > 0.0) degrees of
271
freedom in the numerator and <literal>Dfd</literal> (real
272
> 0.0) degrees of freedom in the denominator. Related
273
function : <link linkend="cdff">cdff</link>.
278
<term>non-central F variance ratio</term>
281
<literal>Y=grand(m,n,"nf",Dfn,Dfd,Xnonc)</literal>
282
generates random variates from the noncentral F (variance
283
ratio) distribution with <literal>Dfn</literal> (real
284
>= 1) degrees of freedom in the numerator, and
285
<literal>Dfd</literal> (real > 0) degrees of freedom in
286
the denominator, and noncentrality parameter
287
<literal>Xnonc</literal> (real >= 0). Related
288
function : <link linkend="cdffnc">cdffnc</link>.
296
<literal>Y=grand(m,n,"gam",shape,scale)</literal>
297
generates random variates from the gamma distribution with
298
parameters <literal>shape</literal> (real > 0) and
299
<literal>scale</literal> (real > 0). The density of the
300
gamma distribution is :
304
\dfrac{ \textrm{scale}^{\textrm{shape}} x^{\textrm{shape}-1} e^{-\textrm{scale} x}}{\gamma(\textrm{shape}) }
308
Related functions : <link linkend="gamma">gamma</link>,
309
<link linkend="cdfgam">cdfgam</link>.
314
<term>Gauss Laplace (normal)</term>
317
<literal>Y=grand(m,n,"nor",Av,Sd)</literal> generates
318
random variates from the normal distribution with mean
319
<literal>Av</literal> (real) and standard deviation
320
<literal>Sd</literal> (real >= 0). Related function
321
: <link linkend="cdfnor">cdfnor</link>.
326
<term>multivariate gaussian (multivariate normal)</term>
329
<literal>Y=grand(n,"mn",Mean,Cov)</literal>
330
generates multivariate normal random
331
variates; <literal>Mean</literal> must be a <literal>m x 1</literal>
332
column vector and <literal>Cov</literal> a <literal>m</literal>-by-<literal>m</literal>
333
symmetric positive definite matrix (<literal>Y</literal> is then
334
a <literal>m</literal>-by-<literal>n</literal> matrix).
339
<term>geometric</term>
342
<literal>Y=grand(m,n,"geom", p)</literal> generates
343
random variates from the geometric distribution with
344
parameter <literal>p</literal> : number of Bernouilli trials
345
(with probability succes of <literal>p</literal>) until a
346
succes is met. <literal>p</literal> must be in
347
<latex>[p_{min},1]</latex> (with <latex> p_{min} = 1{,}3\times 10^{-307} </latex>).
350
<literal>Y</literal> contains positive real numbers
351
with integer values, whiсh are the "number of trials to get
360
<literal>Y=grand(n,"markov",P,x0)</literal> generate
361
<literal>n</literal> successive states of a Markov chain
362
described by the transition matrix
363
<literal>P</literal>. A sum of each the rows in <literal>P</literal> is 1. Initial state is given by <literal>x0</literal>. If <literal>x0</literal> is a
364
matrix of size <literal>m=size(x0,"*")</literal> then
365
<literal>Y</literal> is a matrix of size <literal>m</literal>-by-<literal>n</literal>. <literal>Y(i,:)</literal> is the sample path
366
obtained from initial state <literal>x0(i)</literal>.
371
<term>multinomial</term>
374
<literal>Y=grand(n,"mul",nb,P)</literal> generates
375
<literal>n</literal> observations from the Multinomial
376
distribution : class <literal>nb</literal> events in
377
<literal>m</literal> categories (put <literal>nb</literal>
378
"balls" in <literal>m</literal>
379
"boxes"). <literal>P(i)</literal> is the probability that
380
an event will be classified into category
381
<literal>i</literal>. <literal>P</literal> the vector of probabilities is of
382
size <literal>m-1</literal> (the probability of category
383
<literal>m</literal> is <literal>1-sum(P)</literal>).
384
<literal>Y</literal> is of size <literal>m</literal>-by-<literal>n</literal>.
385
Each column <literal>Y(:,j)</literal> is an observation
386
from multinomial distribution and
387
<literal>Y(i,j)</literal> is the number of events falling in
388
category <literal>i</literal> (for the
389
<literal>j</literal>th observation) (<literal>sum(Y(:,j)) = nb</literal>).
397
<literal>Y=grand(m,n,"poi",mu)</literal> generates
398
random variates from the Poisson distribution with mean
399
<literal>mu</literal> (real >= 0.0). Related function
400
: <link linkend="cdfpoi">cdfpoi</link>.
405
<term>random permutations</term>
408
<literal>Y=grand(n,"prm",vect)</literal> generate
409
<literal>n</literal> random permutations of the column
410
vector (<literal>m x 1</literal>)
411
<literal>vect</literal>.
416
<term>uniform (def)</term>
419
<literal>Y=grand(m,n,"def")</literal> generates
420
random variates from the uniform distribution over
421
<literal>[0,1)</literal> (1 is never return).
426
<term>uniform (unf)</term>
429
<literal>Y=grand(m,n,"unf",Low,High)</literal>
430
generates random reals uniformly distributed in
431
<literal>[Low, High)</literal> (<literal>High</literal> is never return).
436
<term>uniform (uin)</term>
439
<literal>Y=grand(m,n,"uin",Low,High)</literal>
440
generates random integers uniformly distributed between
441
<literal>Low</literal> and <literal>High</literal>
442
(included). <literal>High</literal> and
443
<literal>Low</literal> must be integers such that
444
<literal>(High-Low+1) < 2,147,483,561</literal>.
449
<term>uniform (lgi)</term>
452
<literal>Y=grand(m,n,"lgi")</literal> returns the
453
basic output of the current generator : random integers
454
following a uniform distribution over :
459
<literal>[0, 2^32 - 1]</literal> for mt, kiss and fsultra;
464
<literal>[0, 2147483561]</literal> for clcg2;
469
<literal>[0, 2^31 - 2]</literal> for clcg4;
474
<literal>[0, 2^31 - 1]</literal> for urand.
483
<title>Set/get the current generator and its state</title>
485
The user has the possibility to choose between different base
486
generators (which give random integers following the "lgi" distribution, the others
487
being gotten from it).
494
The Mersenne-Twister of M. Matsumoto and T. Nishimura, period about <literal>2^19937</literal>,
495
state given by an array of <literal>624</literal> integers (plus an index onto this array); this
496
is the default generator.
504
The "Keep It Simple Stupid" of G. Marsaglia, period about <literal>2^123</literal>,
505
state given by <literal>4</literal> integers.
513
A Combined 2 Linear Congruential Generator of P. L'Ecuyer,
514
period about <literal>2^61</literal>, state given by <literal>2</literal> integers.
522
A Combined 4 Linear Congruential Generator of P. L'Ecuyer,
523
period about <literal>2^121</literal>, state given by 4 integers ; this one is
524
split in <literal>101</literal> different virtual (non-overlapping) generators
525
which may be useful for different tasks (see "Actions specific to clcg4" and
526
"Test example for clcg4").
534
A Subtract-with-Borrow generator mixing with a congruential
535
generator of Arif Zaman and George Marsaglia, period more than <literal>10^356</literal>,
536
state given by an array of 37 integers (plus an index onto this array, a flag (0 or 1)
537
and another integer).
545
The generator used by the scilab function <link linkend="rand">rand</link>, state
546
given by <literal>1</literal> integer, period of <literal>2^31</literal>.
547
This generator is based on "Urand, A Universal Random Number Generator" By,
548
Michael A. Malcolm, Cleve B. Moler, Stan-Cs-73-334, January 1973, Computer
549
Science Department, School Of Humanities And Sciences, Stanford University.
550
This is the faster of this list but its statistical qualities are less
551
satisfactory than the other generators.
557
The differents actions common to all the generators, are:
561
<term>action= "getgen"</term>
564
<literal>S=grand("getgen")</literal> returns the current base generator.
565
In this case <literal>S</literal> is
566
a string among "mt", "kiss", "clcg2", "clcg4", "urand", "fsultra".
571
<term>action= "setgen"</term>
574
<literal>grand("setgen",gen)</literal> sets the current base generator to be <literal>gen</literal>
575
a string among "mt", "kiss", "clcg2", "clcg4", "urand", "fsultra".
576
Notice that this call returns the new current generator, i.e. <literal>gen</literal>.
581
<term>action= "getsd"</term>
584
<literal>S=grand("getsd")</literal> gets the current state (the current seeds) of the current base
585
generator ; <literal>S</literal> is given as a column vector (of integers) of dimension <literal>625</literal>
586
for mt (the first being an index in <literal>[1,624]</literal>), <literal>4</literal> for kiss, <literal>2</literal>
587
for clcg2, <literal>40</literal> for fsultra, <literal>4</literal> for clcg4
588
(for this last one you get the current state of the current virtual generator) and <literal>1</literal>
594
<term>action= "setsd"</term>
597
<literal>grand("setsd",S), grand("setsd",s1[,s2,s3,s4])</literal> sets the state of the current
598
base generator (the new seeds) :
605
<literal>S</literal> is a vector of integers of dim <literal>625</literal> (the first component is an index
606
and must be in <literal>[1,624]</literal>, the <literal>624</literal> last ones must be in
607
<literal>[0,2^32[</literal>) (but must not be all zeros) ; a simpler initialisation may be done
608
with only one integer <literal>s1</literal> (<literal>s1</literal> must be in <literal>[0,2^32[</literal>) ;
613
<term>for kiss</term>
616
<literal>4</literal> integers <literal>s1,s2, s3,s4</literal> in <literal>[0,2^32[</literal> must be provided ;
621
<term>for clcg2</term>
624
<literal>2</literal> integers <literal>s1</literal> in <literal>[1,2147483562]</literal> and <literal>s2</literal>
625
in <literal>[1,2147483398]</literal> must be given ;
630
<term>for clcg4</term>
633
<literal>4</literal> integers <literal>s1</literal> in <literal>[1,2147483646]</literal>, <literal>s2</literal>
634
in <literal>[1,2147483542]</literal>, <literal>s3</literal> in <literal>[1,2147483422]</literal>,
635
<literal>s4</literal> in <literal>[1,2147483322]</literal> are required ;
638
<literal>CAUTION</literal> : with clcg4 you set the seeds of the current virtual
639
generator but you may lost the synchronisation between this one
640
and the others virtuals generators (i.e. the sequence generated
641
is not warranty to be non-overlapping with a sequence generated
642
by another virtual generator)=> use instead the "setall" option.
647
<term>for urand</term>
650
<literal>1</literal> integer <literal>s1</literal> in <literal>[0,2^31[</literal> must be given.
655
<term>for fsultra</term>
658
<literal>S</literal> is a vector of integers of dim <literal>40</literal> (the first component
659
is an index and must be in <literal>[0,37]</literal>, the 2nd component is a flag (0 or 1), the 3rd component is
660
an integer in <literal>[1,2^32[</literal> and the 37 others integers are in <literal>[0,2^32[</literal>) ; a simpler (and recommanded)
661
initialisation may be done with two integers <literal>s1</literal> and <literal>s2</literal> in
662
<literal>[0,2^32[</literal>.
670
<term>action= "phr2sd"</term>
673
<literal>Sd=grand("phr2sd", phrase)</literal> given a <literal>phrase</literal> (character string) generates
674
a <literal>1 x 2</literal> vector <literal>Sd</literal> which may be used as seeds to change the state of a
675
base generator (initialy suited for clcg2).
682
<title>Options specific to clcg4</title>
684
The clcg4 generator may be used as the others generators but it offers the advantage
685
to be split in several (<literal>101</literal>) virtual generators with non-overlapping
686
sequences (when you use a classic generator you may change the initial state (seeds)
687
in order to get another sequence but you are not warranty to get a complete different one).
688
Each virtual generator corresponds to a sequence of <literal>2^72</literal> values which is
689
further split into <literal>V=2^31</literal> segments (or blocks) of length <literal>W=2^41</literal>.
690
For a given virtual generator you have the possibility to return at the beginning of the
691
sequence or at the beginning of the current segment or to go directly at the next segment.
692
You may also change the initial state (seed) of the generator <literal>0</literal> with the
693
"setall" option which then change also the initial state of the other virtual generators
694
so as to get synchronisation, i.e. in function of the new initial state of gen <literal>0</literal>
695
the initial state of gen <literal>1..100</literal> are recomputed so as to get <literal>101</literal>
696
non-overlapping sequences.
700
<term>action= "setcgn"</term>
703
<literal>grand("setcgn",G)</literal> sets the current virtual generator for clcg4 (when clcg4
704
is set, this is the virtual (clcg4) generator number <literal>G</literal> which is used); the virtual clcg4
705
generators are numbered from <literal>0,1,...,100</literal> (and so <literal>G</literal> must be an integer
706
in <literal>[0,100]</literal>) ; by default the current virtual generator is <literal>0</literal>.
711
<term>action= "getcgn"</term>
714
<literal>S=grand("getcgn")</literal> returns the number of the current virtual clcg4 generator.
719
<term>action= "initgn"</term>
722
<literal>grand("initgn",I)</literal> reinitializes the state of the current virtual generator
728
<para>sets the state to its initial seed</para>
734
<para>sets the state to its last (previous) seed (i.e. to the beginning of the current segment)</para>
741
sets the state to a new seed <literal>W</literal> values from its last seed (i.e. to the beginning
742
of the next segment) and resets the current segment parameters.
750
<term>action= "setall"</term>
753
<literal>grand("setall",s1,s2,s3,s4)</literal> sets the initial state of generator <literal>0</literal>
754
to <literal>s1,s2,s3,s4</literal>. The initial seeds of the other generators are set accordingly
755
to have synchronisation. For constraints on <literal>s1, s2, s3, s4</literal> see the "setsd" action.
760
<term>action= "advnst"</term>
763
<literal>grand("advnst",K)</literal> advances the state of the current generator by <literal>2^K</literal> values
764
and resets the initial seed to that value.
771
<title>Examples</title>
773
In the following example, we generate random numbers from various distributions and
774
plot the associated histograms.
776
<programlisting role="example"><![CDATA[
777
777
// Returns a 400-by-800 matrix of random doubles,
778
778
// with normal distribution and average 0 and standard deviation 1.
779
779
R = grand(400,800,"nor",0,1);
782
782
xtitle("Normal random numbers from grand","X","Frequency");
784
<scilab:image localized="true">
785
R = grand(400,800,"nor",0,1);
787
xtitle("Normal random numbers from grand","X","Frequency");
790
<programlisting role="example"><![CDATA[
783
791
// Returns a 400-by-800 matrix of random doubles,
784
792
// uniform in [0,1).
785
793
R = grand(400,800,"def");
788
796
xtitle("Uniform random numbers from grand","X","Frequency");
798
<scilab:image localized="true">
799
R = grand(400,800,"def");
801
xtitle("Uniform random numbers from grand","X","Frequency");
804
<programlisting role="example"><![CDATA[
789
805
// Returns a 400-by-800 matrix of random doubles,
790
806
// with Poisson distribution and average equal to 5.
791
807
R = grand(400,800,"poi",5);
794
810
xtitle("Poisson random numbers from grand","X","Frequency");
795
811
]]></programlisting>
797
In the following example, we generate random numbers from the exponential distribution and
798
then compare the empirical with the theoretical distribution.
800
<programlisting role="example"><![CDATA[
803
X = grand(1,N,"exp",lambda);
805
classes = linspace(0,12,25);
808
y = (1/lambda)*exp(-(1/lambda)*x);
810
legend(["Empirical" "Theory"]);
811
xtitle("Exponential random numbers from grand","X","Frequency");
814
In the following example, we generate random numbers from the gamma distribution and
815
then compare the empirical with the theoretical distribution.
817
<programlisting role="example"><![CDATA[
821
R=grand(1,N,"gam",A,B);
822
XS=gsort(R,"g","i")';
824
P=cdfgam("PQ",XS,A*ones(XS),B*ones(XS));
826
plot(XS,PS,"b-"); // Empirical distribution
827
plot(XS,P,"r-"); // Theoretical distribution
828
legend(["Empirical" "Theory"]);
829
xtitle("Cumulative distribution function of Gamma random numbers","X","F");
832
In the following example, we generate 10 random integers in the [1,365] interval.
834
<programlisting role="example"><![CDATA[
812
<scilab:image localized="true">
813
R = grand(400,800,"poi",5);
815
xtitle("Poisson random numbers from grand","X","Frequency");
819
In the following example, we generate random numbers from the exponential distribution and
820
then compare the empirical with the theoretical distribution.
822
<programlisting role="example"><![CDATA[
825
X = grand(1,N,"exp",lambda);
827
classes = linspace(0,12,25);
830
y = (1/lambda)*exp(-(1/lambda)*x);
832
legend(["Empirical" "Theory"]);
833
xtitle("Exponential random numbers from grand","X","Frequency");
835
<scilab:image localized="true">
838
X = grand(1,N,"exp",lambda);
839
classes = linspace(0,12,25);
842
y = (1/lambda)*exp(-(1/lambda)*x);
844
legend(["Empirical" "Theory"]);
845
xtitle("Exponential random numbers from grand","X","Frequency");
848
In the following example, we generate random numbers from the gamma distribution and
849
then compare the empirical with the theoretical distribution.
851
<programlisting role="example"><![CDATA[
855
R=grand(1,N,"gam",A,B);
856
XS=gsort(R,"g","i")';
858
P=cdfgam("PQ",XS,A*ones(XS),B*ones(XS));
860
plot(XS,PS,"b-"); // Empirical distribution
861
plot(XS,P,"r-"); // Theoretical distribution
862
legend(["Empirical" "Theory"]);
863
xtitle("Cumulative distribution function of Gamma random numbers","X","F");
865
<scilab:image localized="true">
869
R=grand(1,N,"gam",A,B);
870
XS=gsort(R,"g","i")';
872
P=cdfgam("PQ",XS,A*ones(XS),B*ones(XS));
874
plot(XS,PS,"b-"); // Empirical distribution
875
plot(XS,P,"r-"); // Theoretical distribution
876
legend(["Empirical" "Theory"]);
877
xtitle("Cumulative distribution function of Gamma random numbers","X","F");
880
In the following example, we generate 10 random integers in the [1,365] interval.
882
<programlisting role="example"><![CDATA[
835
883
grand(10,1,"uin",1,365)
836
884
]]></programlisting>
838
In the following example, we generate 12 permutations of the [1,2,...,7] set.
839
The 12 permutations are stored column-by-column.
841
<programlisting role="example"><![CDATA[
886
In the following example, we generate 12 permutations of the [1,2,...,7] set.
887
The 12 permutations are stored column-by-column.
889
<programlisting role="example"><![CDATA[
842
890
grand(12,"prm",(1:7)')
843
891
]]></programlisting>
846
<title>Get predictible or less predictible numbers</title>
848
The pseudo random number generators are based on deterministic sequences.
849
In order to get reproducible simulations, the initial seed of the generator is constant,
850
such that the sequence will remain the same from a session to the other.
851
Hence, by default, the first numbers produced by <literal>grand</literal> are always the same.
854
In some situations, we may want to initialize the seed of the generator in
855
order to produce less predictible numbers.
856
In this case, we may initialize the seed with the output of the <literal>getdate</literal> function:
858
<programlisting role="example"><![CDATA[
894
<title>Get predictible or less predictible numbers</title>
896
The pseudo random number generators are based on deterministic sequences.
897
In order to get reproducible simulations, the initial seed of the generator is constant,
898
such that the sequence will remain the same from a session to the other.
899
Hence, by default, the first numbers produced by <literal>grand</literal> are always the same.
902
In some situations, we may want to initialize the seed of the generator in
903
order to produce less predictible numbers.
904
In this case, we may initialize the seed with the output of the <literal>getdate</literal> function:
906
<programlisting role="example"><![CDATA[
861
909
]]></programlisting>
864
<title>Test example for clcg4</title>
866
An example of the need of the splitting capabilities of clcg4 is as follows.
867
Two statistical techniques are being compared on data of different sizes. The first
868
technique uses bootstrapping and is thought to be as accurate using less data
869
than the second method which employs only brute force. For the first method, a data
870
set of size uniformly distributed between 25 and 50 will be generated. Then the data set
871
of the specified size will be generated and analyzed. The second method will choose a
872
data set size between 100 and 200, generate the data and analyze it. This process will
873
be repeated 1000 times. For variance reduction, we want the random numbers used in the
874
two methods to be the same for each of the 1000 comparisons. But method two will use more
875
random numbers than method one and without this package, synchronization might be difficult.
876
With clcg4, it is a snap. Use generator 0 to obtain the sample size for method one and
877
generator 1 to obtain the data. Then reset the state to the beginning of the current block
878
and do the same for the second method. This assures that the initial data for method two is
879
that used by method one. When both have concluded, advance the block for both generators.
883
<title>See Also</title>
884
<simplelist type="inline">
886
<link linkend="rand">rand</link>
889
<link linkend="sprand">sprand</link>
892
<link linkend="ssrand">ssrand</link>
912
<title>Test example for clcg4</title>
914
An example of the need of the splitting capabilities of clcg4 is as follows.
915
Two statistical techniques are being compared on data of different sizes. The first
916
technique uses bootstrapping and is thought to be as accurate using less data
917
than the second method which employs only brute force. For the first method, a data
918
set of size uniformly distributed between 25 and 50 will be generated. Then the data set
919
of the specified size will be generated and analyzed. The second method will choose a
920
data set size between 100 and 200, generate the data and analyze it. This process will
921
be repeated 1000 times. For variance reduction, we want the random numbers used in the
922
two methods to be the same for each of the 1000 comparisons. But method two will use more
923
random numbers than method one and without this package, synchronization might be difficult.
924
With clcg4, it is a snap. Use generator 0 to obtain the sample size for method one and
925
generator 1 to obtain the data. Then reset the state to the beginning of the current block
926
and do the same for the second method. This assures that the initial data for method two is
927
that used by method one. When both have concluded, advance the block for both generators.
931
<title>See Also</title>
932
<simplelist type="inline">
934
<link linkend="rand">rand</link>
937
<link linkend="sprand">sprand</link>
940
<link linkend="ssrand">ssrand</link>