Question

python. It is better to use "random"Now we want to analyze how often the tortoise and hare win or lose the race as we vary the race length. We can wrap the logic

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q1:

Sol:

# program in Python to simulate biased winner

import random # for random function
# number of experiments done
race_length = [50,100,250,1000]
# array to hold the outcomes
# note that we have designed this array to have biased results on tortoise
arr = ["tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise","tortoise", "tortoise", "tortoise","tortoise", "tortoise", "tortoise", "tortoise", "tortoise", "tortoise", "tortoise", "tortoise", "tortoise","hare","tie","tie","tie","tie"]
# function to simulate race
def simulate_race(num):
# returns the biased results based on race_length
return arr[random.randint(round((1000-num)/45),34)]
# print the table header
print("-------------------------------------------------------")
print("Experiment | % Tortoise Wins | % Hare Wins | % Ties\n")
print("-------------------------------------------------------")
for length in race_length:
# variables for winners
hareWin = 0;
tortWin = 0;
tie = 0;
# loop 10000 times for simulation
for i in range(10000):
winner = simulate_race(length)
# find the winner and update respective variables
if winner == "hare":
hareWin = hareWin + 1
elif winner == "tortoise":
tortWin = tortWin + 1
else:
tie = tie + 1;
# print the results row wise
print(length , "\t | " , str(round(tortWin*0.01)) , "%\t | " , str(round(hareWin*0.01)) , "%\t | " , str(round(tie*0.01)), "%\n")

# Output

-------------------------------------------------------
Experiment | % Tortoise Wins | % Hare Wins | % Ties

-------------------------------------------------------
50             |  65 %      |  7 %        |  28 %

100        |  67 %          |  6 %        |  27 %

250        |  73 %          |  5 %        |  22 %

1000       |  86 %          |  3 %        |  11 %

# Screenshot of Code (for reference to indentation)

main.py 1 # program in Python to simulate biased winner 2 3 import random # for random function 4 # number of experiments don

(Thank You !!, Please consider upvoting if it was helpful !!)

Add a comment
Know the answer?
Add Answer to:
python. It is better to use "random" Now we want to analyze how often the tortoise...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program to recreate the classic race between the hare and the tortoise. Each animal...

    Create a program to recreate the classic race between the hare and the tortoise. Each animal will move across the screen left to right over the course of 70 squares from 1 - 70 (each will have their own variable valued 1 - 70 and will print as an H or T) . Random numbers will determine the amount each animal will move forward or backward. You will write a program calls functions that uses pointers and pass by reference...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

  • JAVA MASTERMIND The computer will randomly select a four-character mastercode. Each character rep...

    JAVA MASTERMIND The computer will randomly select a four-character mastercode. Each character represents the first letter of a color from the valid color set. Our valid color choices will be: (R)ed, (G)reen, (B)lue and (Y)ellow. Any four-character combination from the valid color set could become the mastercode. For example, a valid mastercode might be: RGBB or YYYR. The game begins with the computer randomly selecting a mastercode. The user is then given up to 6 tries to guess the mastercode....

  • The Binomial and Poisson Distributions Both the Binomial and Poisson Distributions deal with discrete data where...

    The Binomial and Poisson Distributions Both the Binomial and Poisson Distributions deal with discrete data where we are counting the number of occurrences of an event. However, they are very different distributions.  This problem will help you be able to recognize a random variable that belongs to the Binomial Distribution, the Poisson Distribution or neither. Characteristics of a Binomial Distribution Characteristics of a Poisson Distribution The Binomial random variable is the count of the number of success in n trials:   number of...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

  • Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such...

    Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such as declaration, initialization, storing, retrieving, and processing elements. • Effectively manipulating and using ArrayList objects. • Becoming familiar with the use of arrays as method arguments and how array elements are passed to and returned from the called method. • Mastering the use of various debugging tools available on the Eclipse IDU. Important: EVERY one of the following Activities MUST be in a separate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT