1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>RANDN Gaussian (Normal) Random Number Generator
</TITLE>
</HEAD>
<BODY>
<H2>RANDN Gaussian (Normal) Random Number Generator
</H2>
<P>
Section: <A HREF=sec_random.html> Random Number Generation </A>
<H3>Usage</H3>
Creates an array of pseudo-random numbers of the specified size.
The numbers are normally distributed with zero mean and a unit
standard deviation (i.e., <code>mu = 0, sigma = 1</code>).
Two seperate syntaxes are possible. The first syntax specifies the array
dimensions as a sequence of scalar dimensions:
<PRE>
y = randn(d1,d2,...,dn).
</PRE>
<P>
The resulting array has the given dimensions, and is filled with
random numbers. The type of <code>y</code> is <code>double</code>, a 64-bit floating
point array. To get arrays of other types, use the typecast
functions.
The second syntax specifies the array dimensions as a vector,
where each element in the vector specifies a dimension length:
<PRE>
y = randn([d1,d2,...,dn]).
</PRE>
<P>
This syntax is more convenient for calling <code>randn</code> using a
variable for the argument.
Finally, <code>randn</code> supports two additional forms that allow
you to manipulate the state of the random number generator.
The first retrieves the state
<PRE>
y = randn('state')
</PRE>
<P>
which is a 625 length integer vector. The second form sets
the state
<PRE>
randn('state',y)
</PRE>
<P>
or alternately, you can reset the random number generator with
<PRE>
randn('state',0)
</PRE>
<P>
<H3>Function Internals</H3>
Recall that the
probability density function (PDF) of a normal random variable is
<P>
<DIV ALIGN="CENTER">
<IMG SRC="randn_eqn1.png">
</DIV>
<P>
The Gaussian random numbers are generated from pairs of uniform random numbers using a transformation technique.
<H3>Example</H3>
The following example demonstrates an example of using the first form of the <code>randn</code> function.
<PRE>
--> randn(2,2,2)
ans =
(:,:,1) =
-1.7375 -0.5664
-0.2634 -1.0112
(:,:,2) =
-0.4020 0.0557
-1.8966 0.2098
</PRE>
<P>
The second example demonstrates the second form of the <code>randn</code> function.
<PRE>
--> randn([2,2,2])
ans =
(:,:,1) =
-0.7183 1.9415
0.1010 -1.1747
(:,:,2) =
0.3048 3.1685
-1.4185 -0.6130
</PRE>
<P>
In the next example, we create a large array of 10000 normally distributed pseudo-random numbers. We then shift the mean to 10, and the variance to 5. We then numerically calculate the mean and variance using <code>mean</code> and <code>var</code>, respectively.
<PRE>
--> x = 10+sqrt(5)*randn(1,10000);
--> mean(x)
ans =
10.0135
--> var(x)
ans =
4.9458
</PRE>
<P>
Now, we use the state manipulation functions of <code>randn</code> to exactly reproduce
a random sequence. Note that unlike using <code>seed</code>, we can exactly control where
the random number generator starts by saving the state.
<PRE>
--> randn('state',0) % restores us to startup conditions
--> a = randn(1,3) % random sequence 1
a =
-0.0362 -0.1404 0.6934
--> b = randn('state'); % capture the state vector
--> c = randn(1,3) % random sequence 2
c =
0.5998 0.7086 -0.9394
--> randn('state',b); % restart the random generator so...
--> c = randn(1,3) % we get random sequence 2 again
c =
0.5998 0.7086 -0.9394
</PRE>
<P>
</BODY>
</HTML>
|