0% found this document useful (0 votes)
41 views11 pages

Generating Pseudo-Random Numbers in Matlab Or, How To Use Rand and Randn

This document provides an overview of using the rand and randn functions in Matlab to generate pseudo-random numbers. It explains that rand generates uniformly distributed random numbers between 0 and 1, while randn generates normally distributed random numbers with mean 0 and standard deviation 1. Examples are given of using these functions to generate random vectors and plotting histograms to visualize the distributions. Tips are also provided on modifying the mean and standard deviation of randn outputs and ensuring random numbers don't repeat by using the rng(shuffle) command.

Uploaded by

riadelectro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views11 pages

Generating Pseudo-Random Numbers in Matlab Or, How To Use Rand and Randn

This document provides an overview of using the rand and randn functions in Matlab to generate pseudo-random numbers. It explains that rand generates uniformly distributed random numbers between 0 and 1, while randn generates normally distributed random numbers with mean 0 and standard deviation 1. Examples are given of using these functions to generate random vectors and plotting histograms to visualize the distributions. Tips are also provided on modifying the mean and standard deviation of randn outputs and ensuring random numbers don't repeat by using the rng(shuffle) command.

Uploaded by

riadelectro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Generating Pseudo-random Numbers in Matlab

or,
How to use rand and randn
Gerald W. Recktenwald
Department of Mechanical Engineering
Portland State University
[email protected]

ME 350: using rand and randn


Why Use Statistics in ME 350?

• It’s important for engineers to understand statistics


• Some numerical methods rely on statistical properties and methods
. Least squares curve fitting
. Monte-Carlo methods
. Machine learning
. Signal processing
• Using random inputs can be useful to
. Demonstrate algorithms, i.e., as a teaching device
. To generate test inputs, i.e., don’t rely on “simple” or “nice” values for testing

ME 350: using rand and randn page 1


rand and randn

A = rand(m,n)
• A is a matrix with m rows and n columns.
• Elements of A are approximately a uniform random distribution on [0, 1].

B = randn(m,n)
• B is a matrix with m rows and n columns.
• Elements of B are approximately a normal distribution with mean of 0 and standard
deviation of 1.
The probability density for the (ideal) normal distribution is
!
2
1 (x − µ)
p(x) = √ exp −
2σ 2π 2σ 2

where µ is the mean and σ is the standard deviation.

ME 350: using rand and randn page 2


Examples of rand and randn

x = rand(500,1); x = randn(500,1);
histogram(x); histogram(x);

ME 350: using rand and randn page 3


How to Make a Histogram

Given a vector x

1. Find minimum and maximum: xmin and xmax


2. Divide the range of x into n bins

xmax − xmin
∆x =
n

3. Count the number of x values in each bin


4. Make a bar chart showing the number of counts for each bin

Histograms tell us how the values in x are distributed over the range of x.

Histograms show us frequency distributions, not the shape of y = f (x).

ME 350: using rand and randn page 4


Sample Histogram

Left Right
edge edge Counts
-3.0 -2.5 2
-2.5 -2.0 4 97 values between
0 and 0.5
-2.0 -1.5 24 bin [0, 0.5)
-1.5 -1.0 62
-1.0 -0.5 71 62 values between
–1.5 and –1.0
-0.5 0.0 87
0.0 0.5 97
bin [–1.5, –1.0)
0.5 1.0 69
1.0 1.5 48
1.5 2.0 21
2.0 2.5 9
2.5 3.0 5
3.0 3.5 1

ME 350: using rand and randn page 5


More Histograms: Shape tells us something

ME 350: using rand and randn page 6


randn Samples are not Perfect

Especially for small sample sizes, the distributions from randn will not look like a classical
normal distribution. That’s OK. The output should be random, not uniformly normal.

Here are six consecutive distributions created with 30 samples from randn

ME 350: using rand and randn page 7


randn distributions look more normal as n increases

Results for three runs of randn(n,1) at each n.

n = 10 n = 100 n = 1000 n = 10000

ME 350: using rand and randn page 8


Easily change center (x̄) and spread (σ) of randn distributions

randn produces pseudo-random


distributions with a mean of 0
and a standard deviation of 1.

We can modify the output from


randn to create distributions with
any x̄ and σ we want.

Use this:
x = xbar + sig*randn(n,1)

xbar is the desired mean and sig is the desired standard deviation.

Since the output from randn is not a perfect normal distribution,


mean(x) 6= xbar and std(x) 6= sig.

ME 350: using rand and randn page 9


rand and randn Repeat the “Random” Numbers

The seed values use to initialize the pseudo-random number generators are reused at the
start of each Matlab session, unless you explicitly tell Matlab not to start at the same
values.

The sequence of random values generated by rand and randn repeats unless you issue the

rng(’shuffle’)

command after startup.

ME 350: using rand and randn page 10

You might also like