fork download
  1. #include <iostream> //need this by default for cin
  2. #include <math.h> //includes math functions
  3. #include <cmath> //includes basic math
  4. #include <cfloat> //includes floating point numbers
  5. #include <iomanip> //includes setprecision for decimal places
  6. #include <cstdlib> //needed for rand and srand functions
  7. #include <ctime> //needed for time function used to seed generator
  8. #include <climits>
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14. cout << "The purpose of this program is to estimate pi using the monte "
  15. "carlo method and a random number generator" << endl << endl;
  16.  
  17. unsigned seed = time(0);
  18. srand(seed);
  19.  
  20. int trialcount = 0;
  21. int trials;
  22. float accuracy;
  23. const float pi = 3.14159265;
  24.  
  25.  
  26. cout << "The value of PI can be found as the ratio of areas of a circle of radius r located within a square of side 2r" << endl;
  27. cout << "This program runs a MonteCarlo Simulation that generates numbers located randomly within a square" << endl;
  28. cout << "The count of values within the square and the count of numbers within the circle approximate their areas" << endl;
  29. cout << "An input value of radius determines the size of the circle and square" << endl;
  30. cout << "The user specifies how many trials or test runs are desired" << endl << endl;
  31.  
  32. cout << "The true value of PI to 8 decimal places is 3.14159265" << endl << endl;
  33.  
  34. cout << endl;
  35. cout << "How many trials would you like? ";
  36. cin >> trials;
  37. cout << endl << endl;
  38.  
  39. cout << "Square count gives the Total number of random samples (they are within the square)" << endl;
  40. cout << "Circle count gives the number of random samples that also fall within the circle" << endl << endl;
  41.  
  42.  
  43. while (trialcount != trials)
  44. {
  45. accuracy = 0.1;
  46. cout << "Trial " << trialcount + 1 << endl;
  47. cout << "Accuracy \t\t" << "Square Count \t\t" << "Circle Count \t\t" << "Pi" << endl << endl;
  48.  
  49. for (int j = 0; j < 6; j++)
  50. {
  51. accuracy /= 10;
  52. float randpi = 0;
  53. int squarecount = 0;
  54. int circlecount = 0;
  55.  
  56. int stopAt = (INT_MAX >> 8);
  57. for (int i = 0; (randpi >= pi + accuracy || randpi <= pi - accuracy) && i < stopAt; i++)
  58. {
  59. float x = ((float)(rand() % 32768) / 32767);
  60. float y = ((float)(rand() % 32768) / 32767);
  61.  
  62. squarecount++;
  63.  
  64. if ((x * x) + (y * y) <= 1.0 )
  65. {
  66. circlecount++;
  67. }
  68.  
  69. randpi = float(4 * circlecount) / squarecount;
  70. }
  71.  
  72. cout << setprecision(8) << fixed << accuracy << " \t\t" << squarecount << " \t\t" << circlecount << " \t\t" << randpi << endl << endl;
  73. }
  74.  
  75. trialcount++;
  76. }
  77. }
  78.  
  79.  
Success #stdin #stdout 0.8s 15240KB
stdin
5
stdout
The purpose of this program is to estimate pi using the monte carlo method and a random number generator

The value of PI can be found as the ratio of areas of a circle of radius r located within a square of side 2r
This program runs a MonteCarlo Simulation that generates numbers located randomly within a square
The count of values within the square and the count of numbers within the circle approximate their areas
An input value of radius determines the size of the circle and square
The user specifies how many trials or test runs are desired

The true value of PI to 8 decimal places is 3.14159265


How many trials would you like? 

Square count gives the Total number of random samples (they are within the square)
Circle count gives the number of random samples that also fall within the circle

Trial 1
Accuracy 		Square Count 		Circle Count 		Pi

0.01000000 		37 		29 		3.13513517

0.00100000 		163 		128 		3.14110422

0.00010000 		1547 		1215 		3.14156437

0.00001000 		3340271 		2623451 		3.14160252

0.00000100 		14012 		11005 		3.14159298

0.00000010 		8388607 		6589668 		3.14219904

Trial 2
Accuracy 		Square Count 		Circle Count 		Pi

0.01000000 		14 		11 		3.14285707

0.00100000 		205 		161 		3.14146352

0.00010000 		87524 		68743 		3.14167547

0.00001000 		8122 		6379 		3.14159083

0.00000100 		5424 		4260 		3.14159298

0.00000010 		8388607 		6587529 		3.14117908

Trial 3
Accuracy 		Square Count 		Circle Count 		Pi

0.01000000 		28 		22 		3.14285707

0.00100000 		303 		238 		3.14191413

0.00010000 		862 		677 		3.14153123

0.00001000 		44110 		34644 		3.14160061

0.00000100 		8136 		6390 		3.14159298

0.00000010 		8388607 		6588095 		3.14144897

Trial 4
Accuracy 		Square Count 		Circle Count 		Pi

0.01000000 		33 		26 		3.15151525

0.00100000 		219 		172 		3.14155245

0.00010000 		1342 		1054 		3.14157963

0.00001000 		1794 		1409 		3.14158297

0.00000100 		452 		355 		3.14159298

0.00000010 		8388607 		6590257 		3.14247990

Trial 5
Accuracy 		Square Count 		Circle Count 		Pi

0.01000000 		209 		164 		3.13875604

0.00100000 		36482 		28662 		3.14259076

0.00010000 		643 		505 		3.14152408

0.00001000 		16845 		13230 		3.14158511

0.00000100 		723028 		567865 		3.14159346

0.00000010 		8388607 		6589594 		3.14216375