

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A set of lecture slides from cs1112, a computer science course. The topic of this lecture is probability and random numbers. The slides cover the concept of pseudorandom numbers, uniform probability distribution, generating random integers, and simulating the rolling of dice. The slides also introduce the concept of a 1-d array, or vector, and array indexing.
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Previous Lecture: User-defined functions Examples with varying numbers of input and output parameters Local memory space
Today’s Lecture: Probability and random numbers 1-d array—vector More MATLAB graphics
Announcement: P3 posted, due 10/9 at 6pm
October 2, 2008 Lecture 11 2
Random numbers
Pseudorandom numbers in programming Function rand(…) generates random real numbers in the interval (0,1). All numbers in the interval (0,1) are equally likely to occur—uniform probability distribution. Examples: rand(1) one random # in (0,1) 6rand(1) one random # in (0,6) 6rand(1)+1 one random # in (1,7)
October 2, 2008 Lecture 11 4
Which expression(s) below will give a random integer in [1..6] with equal likelihood?
round(rand(1)6) ceil(rand(1)6) Both expressions above
Simulate a fair 6-sided die
October 2, 2008 Lecture 11 25
Algorithm
Repeat the following:
% roll the die
% increment correct “bin”
October 2, 2008 Lecture 11 28
% Simulate the rolling of 2 fair dice totalOutcome= ???
ceil(rand(1)12) ceil(rand(1)11)+ floor(rand(1)*11)+ 2 of the above None of the above
Discover the answer in section this week!
October 2, 2008 Lecture 11 29
1-d array: vector
An array is a named collection of like data organized into rows or columns A 1-d array is a row or a column, called a vector An index identifies the position of a value in a vector
93 92 87 0 90 82 1 2 3 4 5 6
score
October 2, 2008 Lecture 11 32
Array index starts at 1
Let k be the index of vector x, then k must be a positive integer 1<= k <= length(x) To access the k th^ element: x(k)
x 5 .4 .91 -4 -1 7 1 2 3 4 5 6
% Count outcomes of rolling a FAIR die count= zeros(1,6) for k= 1: face= ceil(rand(1,1)*6); if face== count(1)= count(1) + 1; elseif face== count(2)= count(2) + 1; ⋮ elseif face== count(5)= count(5) + 1; else % face is 6 count(6)= count(6) + 1; end end
count 0 0 0 0 0 0 1 2 3 4 5 6
October 2, 2008 Lecture 11 39
Drawing a polygon (multiple line segments)
% Draw a rectangle with the lower-left % corner at (a,b), width w, height h. x= [a a+w a+w a a ]; % x data y= [b b b+h b+h b ]; % y data plot(x, y)
Fill in the missing vector values!
October 2, 2008 Lecture 11 45
Let’s compute colors!
Show “all combinations” of red and blue Assume some kind of granularity—discretize the color value range for red and blue Assume no contribution from green (set to 0)
Program development: Compute the color first; worry about drawing later Decide on granularity, say, ∆=.
October 2, 2008 Lecture 11 55
Example
Write a program fragment that calculates the cumulative sums of a given vector v. The cumulative sums should be stored in a vector of the same length as v. 1, 3, 5, 0 v 1, 4, 9, 9 cumulative sums of v