Java
TOPIC 25 – LOOP APPLICATION: GRAPHICS

AP EXTRA – LOOP APPLICATION: SIMULATION & MATH PROBLEMS

 

AP LESSON WORK

 

 

QUESTION 1


Alter the code from the note to test the odds of flipping at least 8 HEADS during a 10-coin toss test.  Note that solving this problem mathematically is much more difficult than the problem we looked in the note.

QUESTION 2

Given a deck of cards that has 52 cards, what are the odds of:

a)    Randomly drawing 4 consecutive cards of the same value (so four Kings or four Eights)?

b)    Randomly drawing 5 red cards?

In both cases above, write the code to simulate each test.  Then use a loop to repeat the event 1000000 times and calculate the odds of the event happening.



MATH SOLUTIONS

Below are mathematical solutions for the problems above.  Check that the answers of your programs are similar to the solutions below.

MATH SOLUTION FOR 2A


The first card can be any card, so it's good 100% of the time.


The second card has to be one of the 3 remaining cards of the same value.  And there are 51 cards in the deck.  So the odds of getting this are 3 / 51 which is about 5.9%.

 

The third card has to be one of the 2 remaining cards of the same value.  And there are 50 cards in the deck.  So the odds of getting this are 2 / 50 which is 4%.

The fourth card has to be the one remaining card of the same value.  And there are 49 cards in the deck.  So the odds of getting this are 1 / 49 which is about 2%.

 

To calculate the overall odds of this happening, we multiply the individual odds together.

 

Overall odds are 100% x 5.9% x 4% x 2% which equals 0.0047%. 

 

That is a very very rare event.  You'd have to try this over 20000 times to be able to expect this to happen once!

Of course, trying it on a computer 20000 is super easy!  Hurray!

 

MATH SOLUTION FOR 2B

 

There are 26 red cards in a full deck.

 

The first card being red has odds of 26 / 52 which is exactly 50%.

The second card being red has odds of 25 / 51 which is about 49.0%.

 

The third card being red has odds of 24 / 50 which is exactly 48%.

 

The fourth card being red has odds of 23 / 49 which is about 46.9%.

The fifth card being red has odds of 22 / 48 which is about 45.8%.

 

Again, to calculate the overall odds, we multiply individual odds together.

 

Overall odds are 0.50 x 0.49 x 0.48 x 0.469 x 0.458 which equals about 0.02526.  So the odds are about 2.5% of drawing five red cards in a row from a deck.