Write a process that will use the Monte Carlo method to estimate PI as described
ID: 670913 • Letter: W
Question
Write a process that will use the Monte Carlo method to estimate PI as described
below. The Monte Carlo method to estimate probability generates a large number of points then
determines how many points meet a specific condition. Consider the figure below containing a
square with a circle inside of it, both of which are centered on zero.
The area of the circle would be r
2 where r is 1. The area of the square is s2 where s is 2 (-1 to
1). Therefore the ratio of the area of the circle to the area of the square would be /4. Now
suppose we generate random points to fall uniformly in the area of the square (x varies from -1 to
1 and y varies from -1 to 1). We can state ratio of the number of points falling inside the circle
to the total number of the points is equal to the ratio of the area of the circle to the area of the
square.
/4 = (number of points in the circle)/(total number of points)
Therefore can be defined by the equation
= 4 * (number of points in the circle)/(total number of points)
Generate a process that would generate random numbers between –1 and 1 for x and y positions
of 10000 points and count how many points fall with the circle, then determine an estimated
version of . (See below to generate floating point random numbers. This generation is not
needed to describe the process, but to implement the process)
Generating uniformly distributed random floating point numbers.
From Chapter 3, you know that the rand function in C++ generates whole numbers between –
and a maximum number. If we want to generate random floating point numbers with in a given
span we can use the formula
FPRN = span * GRWN/MRN + offset
where FPRN is a floating point random number, span is the maximum floating point number that
should be generated – the minimum floating point number that should be generated, GRWN is
the generated random whole number that the C++ rand() function will produce, MRN is the
maximum number that the rand() function will produce, and offset is the minimum number that
should be generated. For example, suppose we want to generate random floating point numbers
between 0 and 5. The span would be 5, and the offset would be 0 so the formula would be
FPRN = 5.0 * GWRN/MRN + 0
However, if we wanted to generate floating point numbers between -2 and 3, the span would still
be 5 but the offset would now be -2. Therefore the formula would be
FPRN = 5.0 * GWRN/MRN + (-2) or FPRN = 5.0 * GWRN/MRN -2
Similarly if we wanted to generate random floating point numbers between -1.5 and 10, the
formula would be
FPRN = (10.0 - (-1.5)) * GWRN/MRN + (-1.5) or FPRN = (11.5) * GWRN/MRN -1.5
Explanation / Answer
#!/usr/bin/env python import random import math count_inside = 0 for count in range(0, 10000): d = math.hypot(random.random(), random.random()) if d < 1: count_inside += 1 count += 1 print 4.0 * count_inside / countRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.