• Home
  • Textbooks
  • You Can Do It!: A Beginners Introduction to Computer Programming
  • Lotteries, Ciphers and Random Choices

You Can Do It!: A Beginners Introduction to Computer Programming

Francis Glassborow

Chapter 10

Lotteries, Ciphers and Random Choices - all with Video Answers

Educators


Chapter Questions

04:29

Problem 1

The UK Lottery has a game called "Thunderball". In that game, an entry consists of five numbers chosen from 34 ( 1 to 34 ) plus one number, the Thunderball, chosen from 14 (1 to 14). The Thunderball can be a duplicate of one of the five ordinary choices. Write a program that will select your entry to this game.

Alice Simper
Alice Simper
Numerade Educator
03:02

Problem 2

Write a program that will ask you for your choice of six numbers for a lottery entry and then simulate the UK Lottery's Lotto game. In that game six main numbers are chosen from 49 and then the bonus ball is chosen from what is left.

You win the Jackpot if your choice matches the first six. You win a bonus if your pick includes five matches with the six main numbers and your remaining choice matches the bonus ball. You also win lesser amounts if you get five matches, but not the bonus ball, four matches or three matches with the main six. Note that the bonus ball only counts in the special case of five matches with the main numbers.

Your program should report the computer's choice of numbers and how well the player did.

Jennifer Stoner
Jennifer Stoner
Numerade Educator

Problem 3

Write a program where the computer chooses a number from 1 to 100 inclusive at random (your choice as to how). It then invites you to guess the number. If your guess is wrong it will tell you whether your guess was too high or too low. You are allowed six guesses. Note that this is a reasonable game of chance because even with an optimum strategy you cannot be certain of identifying the number in less than seven guesses.

Now write a program where you choose a number between 1 and 100 inclusive and the computer has six guesses. The challenge is to build in enough randomness so that you cannot deleat the computer by knowing how it is guessing. However if you build in too much randomness the computer's chance of winning will be reduced.

Check back soon!

Problem 4

This is an extension of Exercise 2 with some added rules to determine profits.
An entry costs one monetary unit. Matching three balls wins you 20 units, matching four balls wins you 100 units, matching five balls wins you 10000 units, matching five balls and the bonus wins you 250000 units and matching all six balls wins you $\mathbf{1 0} 000000$ units.

Write a program that will ask the user for their choice of six numbers and how many games they want to play. It will then report the results of playing those games with the user's choice of numbers.

You can enhance this program by providing graphical rewards such as loading appropriate files into a Playpen window. You can also add features such as tracking the number of times each number is chosen and displaying a graph of those frequencies in the Playpen.
There are many other enhancements you could add; how far you go is up to you.

Check back soon!
02:23

Problem 5

The following is a brief summary of the rules for the game of craps played with a pair of six-sided dice:

The shooter rolls the dice. The first roll can have any of the following outcomes: 2,3 or 12 (craps) which is an immediate loss, 7 or 11 which is an immediate winner, or a "point" number which can be $4,5,6,8,9$, or 10 . When a point is rolled the dice are repeatedly rolled until either a total of seven results or the total matches the point. If a total of seven is rolled you lose, if point is rolled you win.

Write a computer version that rolls the dice when you press a key and otherwise follows the rules above. So your first roll will give you a win (total of 7 or 11), a loss (total of 2,3 or 12) or a point. If you have a point the computer will roll the dice on each key press until either you lose (because the roll totals 7 ) or you win (because you make your point).

When you emulate this game it is important that you actually emulate each of the pair of dice. Using a spinner with the numbers 1 to 12 would be a quite different game.

Jacob Shpiece
Jacob Shpiece
Numerade Educator