CSC 15 Lab 5b Enhancing the Horse Race. If you have completed lab 5, then currently each horse has an equal chance of winning. In real horse racing events horses are given odds, or probabilities of winning. A probability is a number from 0 to 1 (usually excluding 1, so the largest probability is .9999...). If you do from random import randint,random then calling random() will return a floating point number that's >=0 and <1. "Odds" and "probabilities" are different units of measurement. For example, 1-4 odds translates to .2, or 20% chance of winning. To compute odds from probabilities, the equation is odds = p/(1-p) The odds value should, however, be interpreted as two numbers. For example, if p=.5, then odds = 1.0 according to this equation. But here 1.0 should be interpreted as 1/1, which is to say that the odds are 1-1 (equal chance to win or lose). If p=.2 (20% chance to win), then the odds are .2/.8 which is 1/4, so the odds are 1-4. Thus, unlike probability, odds should be represented by two numbers. We can represent an odds value with a tuple: (1,4) represents 1-4 odds. Note that the odds should only be given in integers, not in floating point numbers. Also, we want to say 1-4 instead of 2-8. That is, the each component of the odds ratio should be divided the greatest common divisor of the two (gcd(2,8) = 2). You should find in sample programs an algorithm to calculate the gcd of two integers. 1. The first part of this week's assignment is to write a function that, given a probability as an argument, return a odds value as a tuple. For example, calling calcodds(.2) should return (1,4) (assuming your function is called "calcodds"). Also, we can assume that the smallest odds is 1-100, so you may round off the floating point numbers before converting. Hint: int(x+.5) converts float x to the nearest integer. To round off to .01, multiply it by 100 first... 2. Now you need to change the horserace program so that every horse can be given a probability of winning. The sum of the probabilities of winning for all the horses must add up to 1. It's easier to assign probabilities instead of odds to each horse. However, when displaying the probability to the average human, it's better to convert it to odds. For example, if you have 5 horses in the race, you can use an array P = [.2,.4,.2,.1,.1] This means, for example, that horse 0 has a 20% chance of winning and horse 1 has 40% chance of winning. Note that the numbers add up to 1. THEY MUST ALWAYS ADD UP TO 1. 2a. Write a function that takes an array as an argument and returns True iff the array contains numbers that add up to 1.0, so calling check(P) will return True 2b. Given such an array P, now you need to generate a random number to determine which of your n horses will win the race. This can be done, in this example, as follows: rn = random(): if rn <.2 then winner is horse 0, otherwise, if rn <.6 (which is .2+.4) then the winner is horse 1, otherwise, if rn <.8 (.6+.2) then the winner is horse 2 if rn <.9 (.8+.1) then the winner is horse 3 if rn <1.0 (.9+.1) then the winner is horse 4 You will, of course, have to generalize this algorithm into a loop that's abstract enough to work for any array P and any number of horses!! 3. After you have determined which horse should win the race, You need to now bias the simulation so that the horse that was picked to be the winner will indeed win! (think from the perspective of the program, not the user of the program. You determine the result then animate it: this is normal). How to do this part is up to your imagination. You can, for example, set the speed of the winning horse to be greater than all the other horses. But the race will no longer look realistic. A better strategy is to increase the (average) speed of the horse if it is not in the lead, and decrease it gradually back to the standard speed if it's already in the lead. Your simulation must display on the screen the odds of each horse winning the race. Do not display the chosen winner to the user, however, until after the race is over!