![]() ![]() Print "num = %-8d Calculated PI = %8.6f Difference = % 9.6f" % \ There's no need to use a subset of your throws tuple, and you haven't added a body to your for loop. Changing the constant from 4 to 4.0 will change this to a floating point calculation: pi = (4.0 * in_circle) / numThrows Since in_circle and numThrows are both integers, this calculation will be performed using integer arithmetic (in Python 2, at least). Integer arithmetic pi = (4 * in_circle) / numThrows You then proceed to calculate the exact same value of hypot for hundreds or even thousands of iterations. These lines are executed only once when you enter the computePI() function. Generating random numbers in the wrong place xPos = random.uniform (-1.0, 1.0) Also how do I print num with left indentation and ensure that all numbers have the required decimal space? Thank you! I am having trouble calling the ComputePI function in the Main function. Read the relevant sections in the book on formatting. There should be plus or minus sign on the difference. The calculated value of π and the difference must be expressed correct to six places of decimal. The number of throws must be left justified. the actual values of your Calculated PI and Difference will be different but close to the ones shown: Computation of PI using Random Numbers Your output will be similar to the following, i.e. You will call the function computePI() with these numbers as input parameters. The quantity Difference in the output is your calculated value of PI minus math.pi. You will compare your result with the value given by math.pi. In your function main() you want to experiment and see if the accuracy of PI increases with the number of throws on the dartboard. The function computePI() will then return the computed value of PI. ![]() That count divided by the total number of throws is the ratio π/4. You will keep a count of the number of times a dart lands within the circle. You will do this as many times as specified by the number of throws. You will determine if that randomly generated point is inside the circle or not. The function computePI() will simulate the throw of a dart by generating random numbers for the x and y coordinates. Your function main() will call the function computePI() for a given number of throws. It will have the following structure: import math The program that you will be writing will be called CalculatePI. The distance of a point with coordinates ( xPos, yPos ) from the center is math.hypot (xPos, yPos). To determine if a point is inside the circle its distance from the center of the circle must be strictly less than the radius of the circle. The way we achieve that is: xPos = random.uniform (-1.0, 1.0) These values are generated using the random number generator. It has sides that are 2 units long and its center (as well as the center of the inscribed circle) is at the origin.Ī random point inside the dart board can be specified by its x and y coordinates. The upper right corner has coordinates ( 1.0, 1.0) and the lower left corner has coordinates ( -1.0, -1.0 ). Imagine that the square dart board has a coordinate system attached to it. For example, the function uniform(a, b) returns a floating point random number in the range a (inclusive) and b (exclusive). The Random module has several random number generating functions that can be used. To simuluate the throwing of darts we will use a random number generator. The ratio of the area of the circle to the area of the square is π / 4. The area of the dart board is 4 square units. The area of a circle with unit radius is just π square unit. Then the ratio of the number of darts that fall within the circle to the total number of darts thrown is the same as the ratio of the area of the circle to the area of the square dart board. Now imagine that you throw darts at that dart board randomly. The center of the circle coincides with the center of the square. Imagine that you have a dart board that is 2 units square. There is another novel approach to calculate π. The value of π can be estimated from an infinite series of the form: In geometry the ratio of the circumference of a circle to its diameter is known as π. Having trouble with the following question: ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |