-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonte_Carlo_Methods.cs
132 lines (115 loc) · 4 KB
/
Monte_Carlo_Methods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections;
namespace _MSOE_OP_Problems
{
public class Monte_Carlo_Methods
{
public static void Main(string[] args)
{
Console.WriteLine ("MONTE CARLO METHODS");
Console.WriteLine ("Determines approximation of PI by generating random points and taking the percentage of those that lie within the upper right quadrant of the unit circle.\n");
Point[] pointList;
//estimation with 100 points
Console.WriteLine ("Generating 100 points...");
pointList = PointList (100);
Console.WriteLine ("Calculating...");
Console.WriteLine ("For {0} random points:\n# of points within circle: {1}\nPI: {2}\n", pointList.Length, TotalInsideCircle(pointList), PIestimation(TotalInsideCircle(pointList), pointList.Length));
//estimation with 1,000 points
Console.WriteLine ("\nGenerating 1,000 points...");
pointList = PointList (1000);
Console.WriteLine ("Calculating...");
Console.WriteLine ("For {0} random points:\n# of points within circle: {1}\nPI: {2}\n", pointList.Length, TotalInsideCircle(pointList), PIestimation(TotalInsideCircle(pointList), pointList.Length));
//estimation with 10,000 points
Console.WriteLine ("\nGenerating 10,000 points...");
pointList = PointList (10000);
Console.WriteLine ("Calculating...");
Console.WriteLine ("For {0} random points:\n# of points within circle: {1}\nPI: {2}\n", pointList.Length, TotalInsideCircle(pointList), PIestimation(TotalInsideCircle(pointList), pointList.Length));
//estimation with 100,000 points
Console.WriteLine ("\nGenerating 100,000 points...");
pointList = PointList (100000);
Console.WriteLine ("Calculating...");
Console.WriteLine ("For {0} random points:\n# of points within circle: {1}\nPI: {2}\n", pointList.Length, TotalInsideCircle(pointList), PIestimation(TotalInsideCircle(pointList), pointList.Length));
}
protected static Point[] PointList(int size)
{
Point[] points = new Point[size];
return PopulateArray (points);
}
protected static Point[] PopulateArray(Point[] list)
{
//for random numbers
Random generator = new Random ();
for (int i = 0; i < list.Length; i++)
{
//generate random coordinate values
double x = generator.NextDouble ();
double y = generator.NextDouble ();
//create new point and add to list
Point pt = new Point (x, y);
list [i] = pt;
}
return list;
}
protected static int TotalInsideCircle(Point[] list)
{
//go through list and count how many points are inside of circle
int totalInCircle = 0;
for(int x = 0; x < list.Length; x++)
{
if (list [x].IsInQuarterCircle () == true)
{
totalInCircle++;
}
}
return totalInCircle;
}
protected static double PIestimation(double ptsInCircle, double totalPts)
{
return (ptsInCircle/totalPts)*4;
}
public class Point
{
private double xCoor;
private double yCoor;
public Point(double x, double y)
{
xCoor = x;
yCoor = y;
}
public double GetXcoordinate()
{
return xCoor;
}
public double GetYcoordinate()
{
return yCoor;
}
public bool IsInQuarterCircle()
{
//check to see if point lies within the quarter circle (PI/4)
bool isInQcircle = false;
if ((0 <= GetXcoordinate () && GetXcoordinate () <= 0.25) && (0 <= GetYcoordinate () && GetYcoordinate () <= 1))
{
isInQcircle = true;
}
else if ((0 <= GetXcoordinate () && GetXcoordinate () <= 0.5) && (0 <= GetYcoordinate () && GetYcoordinate () <= (7 / 8)))
{
isInQcircle = true;
}
else if ((0 <= GetXcoordinate () && GetXcoordinate () <= 0.75) && (0 <= GetYcoordinate () && GetYcoordinate () <= 0.75))
{
isInQcircle = true;
}
else if ((0 <= GetXcoordinate () && GetXcoordinate () <= (7/8)) && (0 <= GetYcoordinate () && GetYcoordinate () <= 0.5))
{
isInQcircle = true;
}
else if ((0 <= GetXcoordinate () && GetXcoordinate () <= 1) && (0 <= GetYcoordinate () && GetYcoordinate () <= 0.25))
{
isInQcircle = true;
}
return isInQcircle;
}
}
}
}