26. Uniform random numbers

# 26. Uniform random numbers - Uniform Random Numbers The...

This preview shows pages 1–6. Sign up to view the full content.

©2009 by L. Lagerstrom Uniform Random Numbers • The rand function • Pseudo-random numbers • Using the generator state • The round, floor, and ceil functions • Random numbers not between 0 and 1 • Integer random numbers • Simulating die rolls and coin flips • The randi function

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
©2009 by L. Lagerstrom Random Numbers Sequences of random numbers are often used in science and engineering to simulate experimental results. They also can be used in various calculations. There is a whole class of computational algorithms called Monte Carlo methods, for example, that use random numbers in ingenious ways. We therefore need to learn a little bit about how Matlab implements random numbers and how we can use them. Along the way we will simulate coin flips, dice rolls, and a noisy sinusoidal signal. We can have Matlab generate a random number between 0 and 1 (but not including 0 and 1) by using the "rand" function. If we just type "rand" it will return a single random value. Or we can use it to generate an array of random values by specifying the number of rows and columns as parameters. For example, "x = rand(1,500)" will generate a row vector of 500 random values between 0 and 1 and store the results in x, or "x = rand(5,2)" will generate a 5x2 array (5 rows, 2 columns) of random values. (See next slide.)
Matlab code Command window display ©2009 by L. Lagerstrom Using the rand Function %Set format short for display: format short %Generate a random value between %0 and 1: x = rand %A row vector with 3 random values: y = rand(1,3) %A 5x2 array of random values: z = rand(5,2) x = 0.9501 y = 0.2311 0.6068 0.4860 z = 0.8913 0.4447 0.7621 0.6154 0.4565 0.7919 0.0185 0.9218 0.8214 0.7382

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Matlab code Figure window display ©2009 by L. Lagerstrom Uniform Random Numbers % The rand function generates uniform %random numbers between 0 and 1. %"Uniform" means that any number %between 0 and 1 is equally likely to %be generated. A histogram shows this: %Generate a large set of random values %(we'll do a set of 800): x = rand(1,800); %Create and display histogram with %10 bins (since the numbers range from %0 to 1, the bin width will be 0.1): figure(1), clf hist(x) %10 bins is default title('Distrib. of 800 Rand. Numbers') xlabel('Random Number Value') ylabel('Number of Values') %Though there's some variation in the %number of values in each bin (to be %expected since they're random nos., %of course) we see that each of the %10 bins has 80 or so values of the 800 total.
©2009 by L. Lagerstrom Pseudo-random Numbers The random values that Matlab generates are actually what are known as "pseudo-random numbers". What does this mean? When you use rand to generate a sequence of random numbers (e.g., by doing rand(1,800)), the numbers within the sequence are random. That is, given one number, we can't predict what the next number in

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/23/2010 for the course ENG 42325 taught by Professor Lagerstrom during the Spring '10 term at UC Davis.

### Page1 / 20

26. Uniform random numbers - Uniform Random Numbers The...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online