CS1132 Fall 2008 Assignment 1
Adhere to the Code of Academic Integrity. You may discuss background issues and general solution strategies with
others and seek help from course staﬀ, but the homework you submit must be the work of just you. When submitting
your assignment, be careful to follow the instructions summarized in Section 4 of this document.
1
The Key Game
In this contest
n
people have the chance to win a car. There are
n
available keys, one will start the car, while
the others are fake. The players take turns, each selecting a key and trying it in the car. When a player
chooses the correct key, he or she wins the car and the contest is over.
You may be asking yourself: does the order of the players matter? In other words, does each player have an
equal chance of winning? Isn’t it unfair that some players might not even get to choose? We will answer
these questions
experimentally
in this exercise.
1.1
Understanding Probabilities
One way to understand the game is to consider two types of risk each player faces:
•
Opportunity risk: the risk of not getting the chance to choose a key.
•
Chance risk: the risk of choosing a wrong key.
These risks balance each other. Early players have high chance risk and low opportunity risk. Later players
have high opportunity risk and low chance risk because some keys have been eliminated before they choose.
For example, the ±rst player has a 100% chance of getting to choose, but only one chance in
n
of choosing
the correct key. The second player has
n1
chances in
n
of getting to choose, but then has one chance in
n1
of choosing the correct key. The last player has only one chance in
n
of getting to choose, but then has
a 100% chance of choosing the right key. (There will be only one key left at that point.) Overall, the
i
th
player has
(ni+1)/n
chance of playing and
i/(ni+1)
chance of picking the right key. By multiplying these
two probabilities we get the chance of winning for each player, which is
1/n
, i.e. the winning probability is
independent of
i
, which means everyone gets the same chance.
1.2
Simulating one Game
Your ±rst task will be to implement a function
keyGame(n)
that will simulate the game:
function i = keyGame(n)
% n:
the number of players in the game.
%i:
the rank of the winning player.
For example if 3 is returned,
% it means that the players 1 and 2 picked fake keys and player 3
% picked the right key.
Your code must simulate the game: players
1
to
n
take turns picking a key. The simulation stops when a
player has picked the right key. You must pay particular attention that your program maintains the same
probabilities of picking the right key as a real game would. Remember that for the ±rst player the probability
of picking the right key is 1 in n; for the second player it is 1 in n1; while for the last player it is 1 in 1,
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 STAFF
 Bubble Sort, Array, Cocktail sort, right key

Click to edit the document details