Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Computing Pi & Random Numbers Instructor: Joseph DiVerdi, Ph.D., M.B.A.

Similar presentations


Presentation on theme: "Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Computing Pi & Random Numbers Instructor: Joseph DiVerdi, Ph.D., M.B.A."— Presentation transcript:

1 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Computing Pi & Random Numbers Instructor: Joseph DiVerdi, Ph.D., M.B.A.

2 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Calculating the Value of π Use a Square With an Inscribed Circle Area circle = πr 2 Area square = 4r 2 Area circle / Area square = πr 2 / 4r 2 4 * Area circle / Area square = π r

3 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Calculating the Value of π Calculate Areas by Placing & Counting Points –Points Are Either Placed At Random –Just Like a Barroom Dart Board On a Square Lattice –An Orderly and Evenly Spaced Set of Points The Area Is Approximated by the Point Count –To Compute π A Program Must Place A Point Determine If the Point Is in or on the Circle –Every Point Is on the Square Keep Count of the Points Compute The Final Value

4 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Calculating the Value of π Calculate Areas By Placing & Counting Points –The Area Is Approximated By The Number of Points Can Be Performed Either of Two Ways: Random Pattern (Monte Carlo) Regular Pattern (Lattice)

5 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Heart Of The Computation Equation Of A Circle x 2 + y 2 = r 2 sqrt(x 2 + y 2 ) = r If sqrt(x 2 + y 2 ) <= r –Then The Point Is On The Circle (r, r) (r, -r) (-r, r) (-r, -r) (0, 0) (x, y) r

6 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Simplification & Speed Setting r = 1 x 2 + y 2 = 1 2 = 1 If x 2 + y 2 <= 1 Then The Point Is On The Circle –That's Much Faster (1, 1) (1, -1) (-1, 1) (-1, -1) (0, 0)

7 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC More Simplification All Four Quarters Are Identical Only Necessary to Compute One Quarter (1, 1) (1, 0) (0, 0) (0, 1) (1, 1) (1, -1) (-1, 1) (-1, -1) (0, 0)

8 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC More Simplification All Four Quarters Are Identical Only Necessary to Compute One Quarter (1, 1) (1, 0) (0, 0) (0, 1) (1, 1) (1, 0) (0, 1) (0, 0)

9 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Using Lattice Method Compute # Of Points On Each Edge Compute Delta For Each Point Use foreach Loop foreach my $x_point (0..$count - 1) { $x = $x_point / ($count - 1); foreach my $y_point (0..$count - 1) { $x = $y_point / ($count - 1); $area_square++; $area_circle++ if... ; } (1, 0) (1, 1)(0, 1) (0, 0)

10 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Using Lattice Method See Source Code –On Course Web Site

11 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Using Lattice Method 4 points9 points16 points25 points 4 * 3 / 4 = 3.00 4 * 6 / 9 = 2.6666 4 * 11 / 16 = 2.75 4 * 17 / 25 = 2.72

12 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Using Monte Carlo Method We Need To Discuss Random Number Issues (1, 1) (1, 0) (0, 1) (0, 0) while ($area_square < $limit) { $x = some random number $y = some random number $area_square++; $area_circle++ if... ; }

13 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Random Numbers Properties –Defined Over a Specific Interval –Equally Distributed - Equal Probability –NO Correlation Between Subsequent Values Consider a Legal Die (One of a Pair of Dice) –Not Loaded –1 <= i <= 6 –P 1 = 1/6, …, P 6 = 1/6 –If 1, 1, 1, 1, 1, 1 then P 1 = 1/6

14 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Random Numbers in Perl Use srand Once to “Seed” The rand Function –srand time; Is NOT a Good Seed! –srand time ^ ($$ + $$ ^ 16); Is Better Use rand Function to Get a Random Value –Use TrulyRandom Module for Serious Work $float_value1 = rand $limit; $float_value2 = rand $limit; 0 <= $float_value < $limit

15 Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Using Monte-Carlo Method See Source Code –On Course Web Site


Download ppt "Pi & Random Numbers Perl Programming - Fort Collins, CO Copyright © XTR Systems, LLC Computing Pi & Random Numbers Instructor: Joseph DiVerdi, Ph.D., M.B.A."

Similar presentations


Ads by Google