hw1 - CS1132 Fall 2008 Assignment 1 Adhere to the Code of...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
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 staff, 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 n-1 chances in n of getting to choose, but then has one chance in n-1 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 (n-i+1)/n chance of playing and i/(n-i+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 n-1; while for the last player it is 1 in 1,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/08/2010 for the course CS 32 taught by Professor Staff during the Fall '08 term at Cornell University (Engineering School).

Page1 / 4

hw1 - CS1132 Fall 2008 Assignment 1 Adhere to the Code of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online