Lab 5: Discrete Probability Simulations

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/32

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 12:32 AM on 10/17/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

33 Terms

1
New cards

What does the sample() function do?

Explain what: sample(1:2, 1) does.

The sample function randomly chooses one outcome from the Sample Space to simulate a random event like flipping a coin.

sample(1:2, 1) means:

It randomly chooses 1 outcome from the Sample Space: {1, 2} and returns one of the two possible outputs: either a 1 or a 2. 1 represents Heads, 2 would represent Tails.

2
New cards

What does the CoinResults(10) do?

It simulates flipping a coin 10 times and returns all the results.

For example, CoinResults(10) = H T T H H H T H T H.

3
New cards

What does ProbHeads(10) do?

It simulates 10 coin flips and estimates the probability of getting Heads.

We say it estimates the probability of getting heads because it uses the formula for relative frequency to find this: P(Heads) = Number of Heads / Total Flips

4
New cards

Why might the probability for ProbHeads(100) be different on different runs?
For example,

On the first run, ProbHeads(100) might give: 0.57

On the second run, ProbHeads(100) might give 0.45

On the third run, ProbHeads(100) might give 0.51.

Which is correct?

They are actually all correct! This is because they are all estimates from different experiments.

5
New cards

Explain why the Law of Large Numbers applies here:

ProbHeads(10) = 0.6

ProbHeads(100) = 0.54

ProbHeads(1000) = 0.503

ProbHeads(100000) = 0.499

The Law of Large Numbers states that as the number of trials increase, the estimated probability approaches the true probability.

Here, we see that the estimate begins to stabilize near 0.5 (the true probability) the more trials we enter. However, it’ll never quite reach 0.5 because it isn’t perfect, and relies entirely on estimates.

6
New cards

What does replicate() do?

replicate() repeats a function or command a certain number of times, separately.

7
New cards

What does replicate(5, sample(1:2, 10)) do?

This repeats the function: sample(1:2, 10) 5 times, meaning it conducts the coin flipping experiment 5 times.

8
New cards

Why might we use replicate()?

We might use replicate when we want to compare experiments.

9
New cards

What does MaxAndMinHeads() do?

(Note: It is unlikely that you will ever need to use this)

MaxAndMinHeads() runs many coin flipping experiments and reports the largest and smallest proportion of heads observed.

For example, the sample output might look like:

0.58

0.43

This means that across all the experiments, the largest observed proportion of heads was 58% and the smallest was 43%. It indicates how much randomness exists in our experiment.

10
New cards

Fill in the blanks:

A trend to observe is that as the number of coin flips increase, more ________ _______ appear. This is because more coin flips naturally contain more detail.

decimal places

For example:

537/1000 = 0.537

53/100 = 0.53

11
New cards

Fill in the blanks:

More experiments give _____ opportunities to observe unusual outcomes.

more

12
New cards

What does a function like RollSomeDice(ntimes, ndice) do?

Note: The parameters are not given.

(Also Note: It is unlikely you will ever use this.)

It repeatedly rolls a group of dice and counts how many 3s appear.

13
New cards

Explain what each line of the following code is doing:

FlipOnce = function () 
{
    HeadOrTail<-sample(c("Heads", "Tails"), 1)
    return(HeadOrTail)
}


// Defines a function called FlipOnce
FlipOnce = function () 
{

// Creates a variable called HeadOrTail
// c means combine multiple values into one vector/collection
// c is necessary because the sample function only takes 2 arguments
// c("Heads", "Tails") means there are 2 options to choose from.
// 1 represents how many to choose.
// This function chooses 1 option from either Heads or Tails
// and stores the value in HeadOrTail.
    HeadOrTail<-sample(c("Heads", "Tails"), 1)
// Then it returns HeadOrTail.
    return(HeadOrTail)
}


14
New cards

Explain what each line of the following code is doing:

CoinResults = function (n) 
{
    coinList <- c(1:n)
    for(i in 1:n)
    {
     coinList[i] = FlipOnce()
    }
    return(coinList)
}


// Defines a function called CoinResults
// that takes a parameter, n.
CoinResults = function (n) 
{
// coinList is a list that stores all our
// flips from 1 to n.
    coinList <- c(1:n)

// Loops through all of the values from
// 1 to n.
    for(i in 1:n)
    {
// Here, coinList[i] will individually
// store each one of our flips.
// For example: coinList[1] will
// store our first coin flip.
     coinList[i] = FlipOnce()
    }
// returns the list of coin flips.
    return(coinList)
}


15
New cards

Explain what each line of the following code is doing:

ProbHeads = function(n)
{
  coinList<-CoinResults(n)
  numHeads<-sum(coinList == "Heads")
  return(numHeads)
}


// Defines a function called ProbHeads
// that takes an input n.
ProbHeads = function(n)
{
// This means run n coin flips, and
// store all the results in CoinList.
// Remember, CoinResults was our
// function from last time that
// ran FlipOnce() n times, and stored
// each value in the list.
  coinList<-CoinResults(n)
// Creates a variable called numHeads.
// coinList contains a list of:
// Heads, Heads, Tails, Heads, for example,
// so coinList == "Heads" then becomes
// TRUE, TRUE, FALSE, TRUE, and we sum
// up these boolean values to get 1+1+1 =3.
// So numHeads stores the number representing
// the number of Heads.
  numHeads<-sum(coinList == "Heads")
// It returns the number of heads.
  return(numHeads)
}


16
New cards

Describe what the following line is doing:

replicate(5, sample(1:2, 10, repl = T))

It runs this sample function 5 separate times, choosing 50 different random values from our sample space of {1,2} over the course of those 5 times.

17
New cards

Describe what the following line is doing:

replicate(5, ProbHeads(100))

This runs ProbHeads(100) 5 times.

18
New cards

Desribe what the following function does.

What does repl=TRUE mean in the following function?

sum(sample(C(1:20), 5, repl=TRUE) == 10)

sample() chooses 5 random values from the sample space all the way from 1-20. repl == TRUE means repeated random values are allowed. sum() is then used to sum up the number of random values chosen that were equal to 10.

19
New cards

Before starting the next questions, write all of the following equations on a cheat sheet somewhere.

These equations should be memorized for this chapter’s cheat sheet:

(Flip Card to See)

sum(sample(C(1:20), 5, repl=TRUE) == 10)

replicate(5, sample(1:2, 10))

ProbNotFunction = function(n)
{
prob<-(sample(c(1:52), n, repl=TRUE) >=12)/n
return(prob)
}

Note: Something similar to the below formulas will be given on the exam, but they are written in a way which is incredibly confusing.

Addition Rule (OR): P(A ∪ B) = P(A) + P(B) - P(A ∩ B)

Conditional Rule: P(B | A) = P(A ∩ B) / P(A)

Multiplication Rule (AND) (when events are Independent): P(A ∩ B) = P(A) x P(B)

Multiplication Rule (AND) (when events are Dependent): P(A ∪ B) = P(A) x P(B | A)

Rule of Total Probability: P(A1) x P(B | A1) + P(A2) x P(B | A2) + P(A3) x P(B | A3)

Classical Probability: P(A) = |A| / |S|

Relative Probability: P(A) = Number of Times the Event Occurs / Total Number of Trials

Counting Rule (AND scenarios): Number of Options for Pick 1 x Number of Options for Pick 2….


Optional to add (you will probably never use these):

CoinResults(10) = Flips a coin 10 times, gives you H, T, T, T, H, H, H, T, H,….

ProbHeads(10) = Gives you the relative probability of Heads after 10 coin flips

MaxAndMinHeads() = Largest/smallest proportion of Heads

replicate(5, sample(1:2, 10)) = Repeats the function 5 times

20
New cards

In Dungeons & Dragons, players often roll a 20-sided die. This die has the numbers from 1 through 20 (inclusive) inscribed on one side each, and all sides are equally likely to land face-up.

a) If this die is rolled 5 times, what is the (classical) probability that NONE of the rolls is a 20?

The question is asking, what is the probability of:

Roll 1: Not a 20 AND

Roll 2: Not a 20 AND

Roll 3: Not a 20 … and so on till Roll 5.

If we don’t want to roll a 20 on any one of our die rolls, this means, we only have 19 out of 20 options left for each roll. And since the rolls are independent form each other, we have 19/20 options for each and every die roll, it doesn’t decrease by 1 each time.

Roll 1 has 19 options.

Roll 2 has 19 options

Roll 3 has 19 options…and so on.

Using the Counting Rule:

19 × 19 × 19 × 19 × 19 = 2476099

|A| = 2476099

The Total Sample Space:

Roll 1 has 20 options.

Roll 2 has 20 options.

Roll 3 has 20 options…and so on.

Using the Counting Rule:

20 × 20 × 20 × 20 × 20 = 3200000

|S| = 3200000

Using Classical Probability:

P(A) = |A| / |S|

= 2476099 / 3200000 = 0.7738 OR 77.38%.

21
New cards

In Dungeons & Dragons, players often roll a 20-sided die. This die has the numbers from 1 through 20 (inclusive) inscribed on one side each, and all sides are equally likely to land face-up.

b) Give a command or series of commands that simulates rolling this die 5 times and counts the number of 20’s that were rolled. Your answer to this question should NOT be a function, simply a command or series of commands.

sum(sample(c(1:20), 5, repl=TRUE))

*Reference Cheat Sheet for this Question

22
New cards

In Dungeons & Dragons, players often roll a 20-sided die. This die has the numbers from 1 through 20 (inclusive) inscribed on one side each, and all sides are equally likely to land face-up.

What is the (classical) probability that a single roll of this die will be less than 9?

The question is asking, what is the probability of:

Roll 1: Less than 9

If we only want one roll which is less than 9, this means we only have 20-9 = 8 options left for the 1 roll.

Roll 1 has 8 options.

Using the Counting Rule:

8

|A| = 8

The Total Sample Space:

Roll 1 has 20 options.

Using the Counting Rule:

20

|S| = 20

Using Classical Probability:

P(A) = |A| / |S|

= 8 / 20 = 2/5

23
New cards

Write a function ProbLessThanNine(n) that simulates rolling this 20-sided die n times and returns the estimated relative frequency probability of rolling less than 9. Your function should return this estimated probability, as a decimal number.

ProbLessThanNine = function(n)
{
    prob<-sum(sample(c(1:20), n, repl=TRUE) < 9)/n
    return(prob)
}

We want the values that are less than 9, so for our sample function, we add the < 9 check.

n represents the total number of trials conducted. We divide by n to get our relative frequency probability.

24
New cards

My child’s board game has a spinner with the five colours: red, blue, yellow, green and purple. All colours are equally likely to be the result of a single spin.

If this spinner is spun 4 times, what is the (classical) probability that NONE of the spins is blue?

The question is asking, what is the probability of:

Spin 1: Not Blue AND

Spin 2: Not Blue AND

Spin 3: Not Blue…Spin 4.

If we want none of our Spins to be Blue, this means that we only have 4 options left for each of the spins.

Spin 1 has 4 options.

Spin 2 has 4 options.

Spin 3 has 4 options.

Spin 4 has 4 options.

Using the Counting Rule:

4 × 4 × 4 × 4

|A| = 256

The Total Sample Space:

Spin 1 has 5 options.

Spin 2 has 5 options.

Spin 3 has 5 options

Spin 4 has 5 options.

Using the Counting Rule:

5 × 5 × 5 × 5

|S| = 625

Using Classical Probability:

P(A) = |A| / |S|

= 256 / 625 = 0.4096

25
New cards

My child’s board game has a spinner with the five colours: red, blue, yellow, green and purple. All colours are equally likely to be the result of a single spin.

Give a command or series of commands that simulates spinning this spinner 4 times and counts the number of “blue” that are spun. Your answer to this question should NOT be a function, simply a command or series of commands.

sum(sample(c(“red”, “blue”, “yellow”, “green”, “purple”), 4, repl=TRUE) == “blue”)

Note: It is choosing 4 random values from the following Sample Space: {red, blue, yellow, green, purple}

26
New cards

A standard deck of playing cards contains 13 diamonds, 13 hearts, 13 clubs and 13 spades. Assuming that all cards are equally likely to be drawn, and that cards are replaced and reshuffled before the next draw:

What is the (classical) probability that a single card drawn will NOT be a heart?

We know there are:

  • 52 cards

  • 4 suits, each containing 13 cards

Therefore there are 13 Hearts, and 52-13= 39 other cards which are not Hearts.

The question is asking, what is the probability of:

Draw 1: Not a Heart.

If we want none of our cards to be Hearts, this means we only have 39 options left for this one draw.

Draw 1 has 39 options.

Using the Counting Rule:

39

|A| = 39

The Total Sample Space:

Draw 1 has 52 options.

Using the Counting Rule:

52

|S| = 52

Using Classical Probability:

P(A) = |A| / |S|

= 39 / 52 = 0.75

27
New cards

A standard deck of playing cards contains 13 diamonds, 13 hearts, 13 clubs and 13 spades. Assuming that all cards are equally likely to be drawn, and that cards are replaced and reshuffled before the next draw:

Write a function ProbNotHeart(n) that simulates drawing a card from a standard deck of cards n times with replacement and returns the estimated relative frequency probability of NOT drawing a heart. Your function should return this estimated probability, as a decimal number.

ProbNotHeart = function(n) 
{ 
probability = sum(sample(c(1:52), n, repl=TRUE)>=14)/n 
// Note: We assume
// Cards 1-13 are Hearts
// Cards 14-26 are another Suit
// Cards 27-39 are another Suit
// Cards 40-52 are another Suit
return(probability) 
}


28
New cards

A standard deck of 52 playing cards contains 4 Jacks and 4 Kings (and 44 cards that are neither of these).

Write a function Quiz4Sim(n) that simulates drawing a card from a standard deck of cards n times with replacement and returns the estimated relative frequency probability of a single card draw being neither a Jack nor a King. Your function should return this estimated probability, as a decimal number

Quiz4Sim = function(n)
{
    prob<-sum(sample(c(1:52), n, repl=True) >=9)/n
    return(prob)
}


29
New cards

A box of 12 doughnuts contains 3 chocolate glazed, 4 apple fritters and 5 Boston cream doughnuts. If two doughnuts are chosen at random to eat (so without replacement), with is the probability that:

a) Both doughnuts are chocolate glazed

We know we have a box of 12 donuts.

We’re being asked for the probability that:

Donut One is Choco

AND

Donut Two is Choco.

We know that we have:

  • 12 donuts total

  • 3 Choco

  • And that the events are not independent, because drawing one donut from the box will impact the outcome of choosing another donut.

So we know that:

Pick One has 3 Choco options

Pick Two has 2 Choco options

Using the Counting Rule:

3 × 2 = 6

|A| = 6

The Total Sample Space:

Pick One has 12 donut options.

Pick Two has 11 donut options.

Using the Counting Rule:

12 × 11

|S| = 132

Using Classical Probability:

P(A) = |A| / |S|

= 6 / 132

30
New cards

A box of 12 doughnuts contains 3 chocolate glazed, 4 apple fritters and 5 Boston cream doughnuts. If two doughnuts are chosen at random to eat (so without replacement), with is the probability that:

b) Neither doughnut is chocolate glazed

We know we have a box of 12 donuts.

We’re being asked for the probability that:

Donut One is not Choco

AND

Donut Two is not Choco.

We know that we have:

  • 12 donuts total

  • 3 Choco

  • And that the events are not independent, because drawing one donut from the box will impact the outcome of choosing another donut.

So we know that there are only 12-3 = 9 options left after removing Choco.

Pick One has 9 options

Pick Two has 8 options

Using the Counting Rule:

9 × 8 = 72

|A| = 72

The Total Sample Space:

Pick One has 12 donut options.

Pick Two has 11 donut options.

Using the Counting Rule:

12 × 11

|S| = 132

Using Classical Probability:

P(A) = |A| / |S|

= 72 / 132

31
New cards

A box of 12 doughnuts contains 3 chocolate glazed, 4 apple fritters and 5 Boston cream doughnuts. If two doughnuts are chosen at random to eat (so without replacement), with is the probability that:

c) One of the doughnuts is chocolate glazed and the other one is an apple fritter.

We know we have a box of 12 donuts.

We’re being asked for the probability that:

Donut One is not Choco

AND

Donut Two is not Choco.

We know that we have:

  • 12 donuts total

  • 3 Choco

  • 4 Apple

  • And that the events are not independent, because drawing one donut from the box will impact the outcome of choosing another donut.

We have two possible outcomes/events here:

  • The event that Pick One is Choco and Pick Two is Apple

  • The event that Pick One is Apple and Pick Two is Choco

The event where Pick One=Choco and Pick Two=Apple:

Pick One has 3 options

Pick Two has 4 options

Using the Counting Rule:

3 × 4 = 12

|A| = 12

The event where Pick One=Apple and Pick Two=Choco:

Pick One has 4 options

Pick Two has 3 options

Using the Counting Rule:

4 × 3 = 12

|A| = 12

The Total Sample Space:

Pick One has 12 donut options.

Pick Two has 11 donut options.

Using the Counting Rule:

12 × 11

|S| = 132

Using the Addition Rule:

12/132 + 12/132 - 0 = 24/132

Answer: 24 / 132

Note: they never intersect, you can never have both Choco and Apple at the same time, so the intersection = 0.

32
New cards

Sandi has a box of 67 AA batteries: 22 of these batteries are Duracell brand and 45 of them are Amazon basics brand. 80% of the Duracell batteries are expired and 40% of the Amazon basics brand batteries are expired.

a) If we randomly choose a battery from this box, what is the probability that it is expired?

Using the Rule of Total Probability:

A1 is Duracell
The probability of being Duracell is 22/67
The probability of Duracell being expired is 0.8

A2 is Amazon
The probability of being Amazon is 45/67
The probability of Amazon being expired is 0.4

Total Probability:

P(A) = P(A1) x P(B | A1) + P(A2) x P(B | A2)

P(of being expired) = (22/67)(0.8) + (45/67)(0.4)

= 53.13%

33
New cards

Sandi has a box of 67 AA batteries: 22 of these batteries are Duracell brand and 45 of them are Amazon basics brand. 80% of the Duracell batteries are expired and 40% of the Amazon basics brand batteries are expired.

b) Sandi grabs a random battery from the box and it turns out to be expired. What is the probability that it is a Duracell brand battery?

The question is essentially asking us what the probability of being expired is given Duracell. So we can use the formula for conditional Probability.

P(B|A) = P(A ∩ B) / P(A)

P(of being expired given Duracell) = P(of being Duracell and expired) / total probability of being expired

P(of being expired given Duracell) = (22/67)(0.8) / (22/67)(0.8)+(45/67)(0.4)

= 49.44%

Note: A ∩ B represents where they intersect.