Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This lab will progressivley develop a program to simulate a horse race. 1) We wi

ID: 3816976 • Letter: T

Question

 This lab will progressivley develop a program to simulate a horse race.  1) We will need a random number generator that can generate random integers within a given range. Fortunately, one comes along with Python called randrange.  It is part of the package random and random.randrange(a, b) generates random integers which are >= a and strictly < b.  Thus random.randrange(3, 6) could give 3, 4, or 5.  Each of these values is (roughly) equally likely  a) write a python program to experiment with this function. Start with import random  Then use a loop to generate 1000 random integers in the range 10 to 20 (inclusive) randrange(10, 21) will do this. Find the average of your 1000 random integers.  It should be close to 15.  b) Write a program to simulate a horse race with one horse.  Assume that the race track is 2 miles long.  Your horse can run at most 40 feet in one second, but for any given second may run any number of feet between 4 and 40. Your program should have a loop that calculates the horses position at the end of every second until the horse crosses the finish line.  Each second, generate a random integer and add it tot he horses current position.  The output should be the number of seconds required to complete the race.  c) Modify the program b) above to run 1000 races, and calculate the average number of seconds to win the race.  d) Modify the program b) above to accomodate more than one horse.  The user should input the number of horses. Each second, generate a different random integer for each horse to indicate its progrerss during that second. The race ends when one (or more) of the horses cross the finish line.  If 2 or more horses cross during the same second, the winner is the one that ends up farthest beyond the finish line.  e) Modify the program further to allow the user to enter the horses names.  Continue entering names until the name XXX is entered.  Do not enter the number of horses, rather count the number of names entered.  As the race is run, at each 10 seconds, the program should announce the position of each horse by name.  When the race concludes, announce the name of the winner.l 

Explanation / Answer

Following the answers of 1a, 1b, 1c, 1d:

#############################################################

# 1 a - Identifying the average of 1000 average number
#!usr/bin python
from random import randint
average = 0
i=1000;
addition = 0;
while i!=0:
addition = addition + randint(10,21);
i-=1;
  
average = addition/1000;
print(average);

#############################################################

#############################################################
#1 b - Write a program to simulate a horse race with one horse.
#!usr/bin python
from random import randint
randNumber=0;
timeTaken=0;
currentPosition=0;

while currentPosition<10560:
currentPosition = currentPosition + randint(4,41);
timeTaken+=1;
print ('time taken to cross 2 miles is',timeTaken, 'seconds');

#############################################################

#############################################################
#1 c - To run 1000 races, and calculate the average number of seconds to win the race
#!usr/bin python
from random import randint
randNumber=0;
timeTaken=0;
currentPosition=0;
totalTime = 0;
averageTime = 0;

for i in range(1,1000):
while currentPosition<10560:
currentPosition = currentPosition + randint(4,41);
timeTaken+=1;
totalTime=totalTime+timeTaken;
averageTime = totalTime/1000;
print ('average time for 1000 races is',averageTime, 'seconds');

#############################################################

#############################################################

#1 d - accomodate more than one horse
#!usr/bin python
from random import randint
import sys
horse = {}
randNumber={};
timeTaken={};
currentPosition={};
totalTime = {};

i = 0;
noOfHorses=int(sys.argv[1]);
while i!=noOfHorses:
horse[i]=0;
timeTaken[i]=0;
currentPosition[i]=0;
i+=1;
check=0;
i=0;
totalMiles = 10560;
while(1):
for i in range(0,noOfHorses):
currentPosition[i] = currentPosition[i] + randint(4,41);
if(currentPosition[i] >= 10560):
   check=1;    
   break
timeTaken[i]+=1;
if(check==1):
   break

print 'race is finished with following details:'

for i in range(0,noOfHorses):
print 'horse no.',i, 'distance covered-',currentPosition[i],'in time ',timeTaken[i];

#############################################################

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote