Question

In: Computer Science

Python program to simulate breaking a unit-length pencil into two places. Repeat the experiment 100,000 times...

Python program to simulate breaking a unit-length pencil into two places. Repeat the experiment 100,000 times and determine the average size of the smallest, middle-size, and the largest pieces. Record the estimate of the average sizes in your program

Please use comments to better explain what is happening in the code

Solutions

Expert Solution

Thanks for the question.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks!
===========================================================================

import random

def main():
    pencil_length = 1
    smallest_length_total = 0
    middle_size_length_total = 0
    largest_size_length_total = 0
    experiments = 100000
    # loop 100000 times
    for experiment in range(experiments):
        break_p1 = random.random()  # generate a random number 0 to 1, this is the first part
        remaining_length = pencil_length - break_p1  # pencil that remains
        break_p2 = random.random() * remaining_length  # break the remaining pencil at random point
        first_part = break_p1  # this is the first part length
        second_part = break_p2  # this is the second part length
        third_part = remaining_length - break_p2  # this is the third part length
        lengths = [first_part, second_part, third_part]  # to quick arrange the lengths in ascending order
        lengths.sort()  # sort the lengths in ascending order
        smallest_length_total += lengths[0]  # add the length to the total
        middle_size_length_total += lengths[1]  # add the length to the total
        largest_size_length_total += lengths[2]  # add the length to the total

    average_smallest = smallest_length_total / experiments  # calculate teh average lengths
    average_middle = middle_size_length_total / experiments  # calculate teh average lengths
    average_largest = largest_size_length_total / experiments  # calculate teh average lengths

    print('Average length of smallest pieces:{:.5f}'.format(average_smallest))  # display
    print('Average length of middle pieces  :{:.5f}'.format(average_middle))
    print('Average length of largest pieces :{:.5f}'.format(average_largest))


if __name__ == '__main__':
    main()

======================================================================


Related Solutions

Python program to simulate estimate the probability that a random chord on a unit-circle (radius one),...
Python program to simulate estimate the probability that a random chord on a unit-circle (radius one), exceeds the radius of the circle? Repeat the experiment of generating random chords 1,000 times. Record the estimate of the probability that the chord length exceeds the radius Please use comments to help explain
Simulate the experiment of rolling two dices for 1000 times. Generate the probability mass distributionfunction(PMF) for...
Simulate the experiment of rolling two dices for 1000 times. Generate the probability mass distributionfunction(PMF) for the sum of the two dices and plot the result using histogramcommand. Use two different methods for your simulations: Using for loops Using vectors and matrices in MATLAB
Write a Python program that reads in two times, an earlier time and a later time,...
Write a Python program that reads in two times, an earlier time and a later time, and prints the difference between the two times in minutes as well as in hours/minutes. The format of the time is HH:MMAM or H:MMAM or HH:MMPM or H:MMPM. Also the AM or PM may be in lower case. A sample run of the program is shown below: $ python3 Time.py Enter Earlier Time: 9:36aM Enter Later Time: 6:22PM Number of minutes between 9:36aM and...
Write a program in Python to simulate a Craps game: 1. When you bet on the...
Write a program in Python to simulate a Craps game: 1. When you bet on the Pass Line, you win (double your money) if the FIRST roll (a pair of dice) is a 7 or 11, you lose if it is ”craps” (2, 3, or 12). Otherwise, if it is x ∈ {4, 5, 6, 8, 9, 10}, then your point x is established, and you win when that number is rolled again before ’7’ comes up. The game is...
Python: Write a program to simulate the purchases in a grocery store. A customer may buy...
Python: Write a program to simulate the purchases in a grocery store. A customer may buy any number of items. So, you should use a while loop. Your program should read an item first and then read the price until you enter a terminal value (‘done’) to end the loop. You should add all the prices inside the loop. Add then a sales tax of 8.75% to the total price. Print a receipt to indicate all the details of the...
Python program that simulates the rolling a die until the number 5 is first obtained. Repeat...
Python program that simulates the rolling a die until the number 5 is first obtained. Repeat the experiment 10,000 times and print out the “Average Number of Rolls to Obtain a Six on a Die”, along with the average value. Include the average number of rolls your program calculates as a comment Please use comments to better explain what is happening
For Exercises use random numbers to simulate the experiments. The number in parentheses is the number of times the experiment should be done.
For Exercises use random numbers to simulate the experiments. The number in parentheses is the number of times the experiment should be done.Two coins are tossed. Find the average number of times two tails will appear. (40) 
Write a Python program to simulate a very simple game of 21 •Greet the user. •Deal...
Write a Python program to simulate a very simple game of 21 •Greet the user. •Deal the user a card and display the card with an appropriate message. •Deal the user another card and display the card .•Ask the user if they would like a third card. If so, deal them another card and display the value with an appropriate message. •Generate a random number between 10 and 21 representing the dealer’s hand. Display this value with an appropriate message....
Write a program to simulate an experiment flipping three coins. Each time the three coins are...
Write a program to simulate an experiment flipping three coins. Each time the three coins are flipped, is called a “trial”. A coin flip can randomly result in either “Heads” (1) or “Tails” (0) Allow the user to enter the number of “trial”s to simulate. Print out each coins’ result after each “trial”. Use seed value 1234. Tally up and determine what percentage of “trial”’s all three coins land on “Heads” in the simulation. C language
(PYTHON) A beard-second is a unit of length inspired by the light-year and is defined as...
(PYTHON) A beard-second is a unit of length inspired by the light-year and is defined as the length the average beard grows in one second, or precisely 5 nanometers. The beard-second is used for extremely short distances such as measurements within an integrated circuit. Your manufacturing equipment is very accurate but it uses inches as a unit of measurement, so you need a conversion table between beard-seconds and inches to complete a special order. The following information should be helpful:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT