1 <?xml version="1.0" encoding="UTF-8"?>
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) 2008 - INRIA
5 * Copyright (C) 2011 - DIGITEO - Michael Baudin
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
17 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
18 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
19 xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
20 xmlns:scilab="http://www.scilab.org" xml:id="rand" xml:lang="en">
22 <refname>rand</refname>
23 <refpurpose>Random numbers</refpurpose>
30 r = rand(m1,m2,...,key)
42 <title>Arguments</title>
45 <term>m1, m2, ...</term>
48 integers, the size of the random matrix <literal>r</literal>.
56 a string, the distribution of random numbers (default <literal>key="uniform" </literal>).
57 The available values are <literal>"uniform"</literal> and
58 <literal>"normal"</literal>.
66 a real or complex matrix.
67 Only its real or complex content and its dimensions are taken into account.
75 a m1-by-m2-by-... real array of doubles with random entries.
83 a positive integer: the seed (default <literal>s=0</literal>).
90 <title>Description</title>
92 The purpose of this function is to return a matrix of doubles with real or complex
94 Depending on the input arguments, the function can return a random matrix of doubles,
95 or can get or set the distribution of random numbers, or can get or set the seed of
96 the random number generator.
100 <title>Generate random numbers</title>
103 <term>r=rand()</term>
105 <para>returns a 1-by-1 matrix of doubles, with one random value.</para>
109 <term>r=rand(m1,m2)</term>
112 is a random matrix with dimension <literal>m1</literal>-by-
113 <literal>m2</literal>.
118 <term>r=rand(m1,m2,...,mn)</term>
121 returns a random matrix with dimension <literal>m1</literal>-by-
122 <literal>m2</literal>-by-... -by-<literal>mn</literal>.
127 <term>r=rand(a)</term>
130 returns a random matrix with the same size as a.
131 The matrix <literal>r</literal> is real if <literal>a</literal> is a
132 real matrix and <literal>r</literal> is complex if <literal>a</literal> is a
140 <title>Change the distribution of the random numbers</title>
142 The <literal>key</literal> input argument sets the distribution of the
143 generated random numbers.
147 <term>rand("uniform")</term>
150 sets the generator to a uniform random number
151 generator. Random numbers are uniformly distributed in the interval
157 <term>rand("normal")</term>
160 sets the generator to a normal (Gauss-Laplace) random number generator,
161 with mean 0 and variance 1.
166 <term>key=rand("info")</term>
169 return the current distribution of the random generator ("uniform" or
177 <title>Get or set the seed</title>
179 It is possible to get or set the seed of the random number generator.
183 <term>s=rand("seed")</term>
185 <para>returns the current value of the seed.</para>
189 <term>rand("seed",s)</term>
192 sets the seed to <literal>s</literal> (by default
193 <literal>s=0</literal> at first call).
200 <title>Examples</title>
202 In the following example, we generate random doubles with
203 various distributions.
205 <programlisting role="example"><![CDATA[
206 // Get one random double (based on the current distribution)
208 // Get one 4-by-6 matrix of doubles (based on the current distribution)
210 // Get one 4-by-6 matrix of doubles with uniform entries
211 r=rand(4,6,"uniform")
212 // Produce a matrix of random doubles with the same size as x
215 // Produce a 2-by-2-by-2 array of random doubles
219 In the following example, we change the distribution of the
220 number generated by <literal>rand</literal>.
221 We first produce normal numbers, then numbers uniform in [0,1).
223 <programlisting role="example"><![CDATA[
224 // Set the rand generator to normal
227 // Get the current distribution
229 // Set the rand generator to uniform
235 In the following example, we generate a 2-by-3 complex matrix
236 of doubles, with normal distribution.
238 <programlisting role="example"><![CDATA[
239 // Produce a 2-by-3 matrix of random complex doubles
240 x=rand(2,3)+%i*rand(2,3)
241 // Produce a matrix of random complex doubles with
242 // normal entries and the same size as x
246 In the following example, we plot the distribution of uniform
247 numbers produced by <literal>rand</literal>.
249 <programlisting role="example"><![CDATA[
250 r=rand(1000,1,"uniform");
253 xtitle("Uniform numbers from rand","X","Frequency")
256 In the following example, we plot the distribution of standard normal
257 numbers produced by <literal>rand</literal>.
259 <programlisting role="example"><![CDATA[
260 r=rand(1000,1,"normal");
263 xtitle("Normal numbers from rand","X","Frequency")
267 <title>Get predictable or less predictable numbers</title>
269 The "uniform" pseudo random number generator is a deterministic sequence which aims at
270 reproducing a independent identically distributed numbers uniform in the
274 In order to get reproducible simulations, the initial seed of the generator is
275 zero, such that the sequence will remain the same from a session to the other.
276 In other words, the first numbers produced by <literal>rand()</literal> always are : 0.2113249, 0.7560439, ...
279 In some situations, we may want to initialize the seed of the generator in
280 order to produce less predictable sequences. In this case, we may initialize the
281 seed with the output of the <literal>getdate</literal> function:
283 <programlisting role="example"><![CDATA[
289 <title>The generator</title>
291 The <literal>"uniform"</literal> random number generator is described in
292 "Urand, A Universal Random Number Generator" by
293 Michael A. Malcolm, Cleve B. Moler, Stan-Cs-73-334, January 1973, Computer
294 Science Department, School Of Humanities And Sciences, Stanford University.
297 It is a linear congruential generator of the form :
303 where the constants are
304 <table border="0" cellpadding="0">
305 <tr><td>a = 843314861</td></tr>
306 <tr><td>c = 453816693</td></tr>
307 <tr><td>M = 2<superscript>31</superscript></td></tr>
311 According to the authors, this generator is a full length generator, that is to say, its
312 period is M = 2<superscript>31</superscript> = 214748364.
315 The <literal>"normal"</literal> random number generator is based on
316 the Box-Muller method, where source of the uniform random numbers is Urand.
320 <title>The statistical quality of the generator</title>
322 Better random number generators are available from
323 the <link linkend="grand">grand</link> function, in the
324 sense that they have both a larger period and better statistical properties.
325 In the case where the quality of the random numbers matters, we should
326 consider the <literal>grand</literal> function instead.
327 Moreover, the <literal>grand</literal> function has more features.
330 <refsection role="see also">
331 <title>See also</title>
332 <simplelist type="inline">
334 <link linkend="grand">grand</link>
337 <link linkend="ssrand">ssrand</link>
340 <link linkend="sprand">sprand</link>