The program I'm writing is supposed to take a number of tosses entered by the user, this part works.
Then take that and check to see if it is within a circle that is inside of a square. Then take the number that are inside the circle, and divide it by the number of tosses and the answer should be close to 3.14 the more tosses you choose.

here's my code so far, it works all good except the number is not close to pi at all.

--                                                     --
-- Circlepi.adb                                        --
-- This program estimates the area of a circle         --
-- using Monte Carlo Integration.                      --

WITH Ada.Text_IO;
USE Ada.Text_IO;
WITH Ada.Integer_Text_IO;
USE Ada.Integer_Text_IO;
WITH Ada.Float_Text_IO;
USE Ada.Float_Text_IO;
WITH Ada.Numerics.Float_Random;
USE Ada.Numerics.Float_Random;

   Gen : Generator;
   Added_Number : integer;
   Finished_Program, Samples_Inside_Circle : Integer;
   Yes_No : Character;
   Number_Of_Samples: integer;
   X, Y : float;
   estimate_of_pi : float;


     Finished_Program := 0;
     Samples_Inside_Circle := 0;
     Added_Number := 1;
     Estimate_Of_Pi := 0.0;
     Number_Of_Samples := 0;
     X := 0.0;
     Y := 0.0;

   WHILE (Finished_Program = 0) LOOP
         Put("Enter the number of Samples: ");
         Get (Number_Of_Samples);
         FOR I IN 1 .. Number_Of_Samples LOOP
            X := 2.0*Random(Gen) - 1.0;
            Y := 2.0*Random(Gen) - 1.0;
            IF X*X + Y*Y < 1.0 THEN
               Samples_Inside_Circle := Samples_inside_circle + Added_Number;
            END IF;
         END LOOP;
         Estimate_Of_Pi := float (Samples_Inside_Circle) / float (Number_Of_Samples);
         Put ("Estimate of Pi: ");
         Put (Estimate_Of_Pi);
         Put ("Do you want to run it again? (y)/(n)");
         IF (Yes_No = 'n' or Yes_No = 'N') THEN
            Finished_Program := 1;
         END IF;

      END LOOP;
4 Years
Discussion Span
Last Post by noelthefish

ok my maths have not been tested in 20 years but I am sure that what you are producing here should follow this

p = m/n
Pi = (approx) p * 4 <- this is the line you are missing. you only ouput the ratio.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.