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 π. And if they could work out an interactive tutorial (in that third debugging window perhaps,) well, that would really make NodeBox a great starter program for young kids.Having trouble with the following question: The page on variables could be cut in a quarter. The tutorial is very good, with lots of pictures, but feels too long. So, if you’re going to start off, just plug random numbers in! Or start with the code in the sample and flip the numbers up one at a time and watch things change. One thing about NodeBox that helps soften the math pains, though: you’re mostly going to be throwing numbers and shapes together to see what kinds of patterns you can come up with. I think this would go a long way with the newbs! I’m curious why they didn’t label the different areas or add some toolbar buttons to switch between them. You basically have a very simple window with boxes for editing, running and debugging scripts. These are important things! It’s so rare to find lovely programming tools. I see alot of strengths in NodeBox: it has a very complete manual (and the manual isn’t that long,) lines and shapes are beautifully antialiased, gradients are smooth. NodeBox is for Mac platforms and scripts are written in Python. The gallery is full of gorgeous stuff, many come with source code. But rather than just shapes, you get Bezier curves and typography. I call this Star Crash Get Out of The Way, Deirdre. Let’s all agree to be proponents of NodeBox.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |