Docsity

Lecture 11 CS1112 - Probability and Random Numbers, Study notes of Computer Science

Toggle sidebar
bg1
CS1112 Lecture 11 2008/10/2
Lecture slides 1
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)
6*rand(1) one random # in (0,6)
6*rand(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
A
B
C
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)+1
floor(rand(1)*11)+2
2 of the above
None of the above
A
B
C
D
E
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
123 45
6
score
pf2

Partial preview of the text

Download Lecture 11 CS1112 - Probability and Random Numbers and more Study notes Computer Science in PDF only on Docsity!

CS1112 Lecture 11 2008/10/

Lecture slides 1

„ 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

A

B

C

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

A

B

C

D

E

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

CS1112 Lecture 11 2008/10/

Lecture slides 2

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