Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 5 - Loops - Programming Exercises - Page 166: 5.42

Answer

code

Work Step by Step

# 5.42 (Monte Carlo simulation) A square is divided into four smaller regions as shown in # (a). If you throw a dart into the square one million times, what is the probability for # the dart to fall into an odd-numbered region? Write a program to simulate the # process and display the result. (Hint: Place the center of the square in the center of # a coordinate system, as shown in (b). Randomly generate a point in the square and # count the number of times for a point to fall in an odd-numbered region.) import random counter = 0 print("Throwing the dart....") for i in range(0, 1000000): if random.randint(1, 4) % 2 != 0: counter += 1 prob = counter / 1000000 print("The probability that the dart fall in an odd numbered region is", prob)
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.