+ 1
How to find the value of pi by using the random no
plz tell me the code meaning too that u are applying on it.
2 Respostas
+ 11
It's just an elaboration of a so-called Monte Carlo routine. You set a 2D center, let's assume (0, 0). Now you set a radius, let's assume 1. You can then imagine a square being drawn with its edges in (-1, -1), (-1, 1), (1, 1) and (1, -1), and its center in (0, 0). Also, imagine a circle of a radius (r=1), drawn inside the square (with a center at 0, 0)).
All you need to do now is to return random amount of pairs of numbers, let's name them (x, y), each of them in a threshold of (-1, 1). For example: (-0.345, 0.452), (0.66, -0.213) and so on.
All of those pairs will be marked as being inside a square, but some will also be inside a circle and some outside. If you generated enough of them, let's say a thousand, the Pi approximation is the number of all those pairs which are inside the circle divided by the number of all generated and this result times 4.
Now, how to check if a point (representation of a number pair) is inside the circle? Well, its distance from the center has to be lower or equal 1. So:
x² + y² ≤ r² or x² + y² ≤ 1 (r was equal to 1).
So you just check all the points if their x and y values meet this condition and if so you add them to the count of those "inside" the circle. Then you just divide the count by the number of all points generated and multiply the result by 4.
Happy fun-coding!
https://code.sololearn.com/czBAt7x8pBAm/?ref=app
+ 4
Here's a fun way:
http://m.wikihow.com/Calculate-Pi-by-Throwing-Frozen-Hot-Dogs 😂😂😂
You could simulate each throw by using a couple of random numbers (orientation and distance from hotdog center to nearest line), figure out if it's a hit,
repeat lots of times and then:
Pi (approx) = 2 * (total throws) / hits 😎
Been meaning to do this for a while hehe.
Anyone care to try? (real or sim) 😂