Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

132 Chapter 6


Kaleidoscope


The random spiral color graphic from Figure 6-2 looked a bit like
a kaleidoscope. To make it look more like a real kaleidoscope, let’s
add an important feature that our spiral program was missing:
reflections.
In a kaleidoscope,
it’s the positioning of the
mirrors that makes ran-
dom colors and shapes
into a lovely pattern. In
this closing example,
we’re going to mimic the
mirror effect by modify-
ing our RandomSpiral.py
program to “reflect” the
spirals four times on the
screen.

Running the numbers on Yahtzee
If you’re interested in the math behind Yahtzee and why five
of a kind is so rare, here’s a quick explanation. First, there
are five dice, each with six sides, so the number of possible
combinations is 6 × 6 × 6 × 6 × 6 = 6^5 = 7,776. There are
7,776 ways to roll five normal, six-sided dice. To figure out
the probability of rolling five dice with the same face value
(five of a kind), we have to figure out how many possible
Yahtzees there are: five 1s, five 2s, and so on up through
five 6s. So there are six possible Yahtzee hands of five of a
kind that we can roll with our five dice. Divide 6 Yahtzees
by the 7,776 total possible rolls, and you get the probability
that you’ll roll five of a kind: 6/7,776, or 1/1,296.
That’s right: the odds that you’ll roll five of a kind on a
single roll are just 1 out of 1,296. So don’t get discouraged
if you roll for a long time before you get your first five of a
kind. On average, you’ll get one every 1,300 rolls or so. No
wonder they give 50 points for a Yahtzee!
Free download pdf