Program.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MonteCarlo
  7. {
  8. internal class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Decision decision = new Decision();
  13. decision.FindingPi();
  14. decision.FindingAreaMonteCarloPrimer();
  15. decision.FindingAreaMonteCarlo1();
  16. decision.FindingAreaMonteCarlo2();
  17. decision.FindingAreaMonteCarlo3();
  18. decision.FindingAreaMonteCarlo4();
  19. decision.FindingAreaMonteCarlo5();
  20. decision.FindingAreaMonteCarlo6();
  21. Console.ReadKey();
  22. }
  23. }
  24. public class Decision
  25. {
  26. public void FindingPi()
  27. {
  28. int n = 100000;
  29. int k = 0;
  30. double x = 0;
  31. double y = 0;
  32. double height = 2;
  33. double weight = 2;
  34. double s = height * weight;
  35. Random random = new Random();
  36. for (int i = 1; i <= n; i++)
  37. {
  38. x = random.NextDouble();
  39. y = random.NextDouble();
  40. if (Math.Pow(x - 1,2) + Math.Pow(y - 1,2) <= 1)
  41. {
  42. k += 1;
  43. }
  44. }
  45. double Pi = s * k / n;
  46. Console.WriteLine("Результат расчёта Pi:\n" + Pi);
  47. }
  48. public void FindingAreaMonteCarloPrimer()
  49. {
  50. int n = 1000000;
  51. int k = 0;
  52. double x = 0;
  53. double y = 0;
  54. double height = 5;
  55. double weight = 8.5;
  56. double s = height * weight;
  57. Random random = new Random();
  58. for (int i = 1; i <= n; i++)
  59. {
  60. x = random.NextDouble() * weight;
  61. y = random.NextDouble() * height;
  62. if (y < (x*(10 - x))/5 && y > x/3)
  63. {
  64. k += 1;
  65. }
  66. }
  67. double res = s * k / n;
  68. Console.WriteLine("Результат расчёта примера:\n" + res);
  69. }
  70. public void FindingAreaMonteCarlo1()
  71. {
  72. int n = 1000000;
  73. int k = 0;
  74. double x = 0;
  75. double y = 0;
  76. double height = 1;
  77. double weight = 20;
  78. double s = height * weight;
  79. Random random = new Random();
  80. for (int i = 1; i <= n; i++)
  81. {
  82. x = random.NextDouble() * weight - 5;
  83. y = random.NextDouble() * height;
  84. if (y < Math.Sin(x) && y > 0)
  85. {
  86. k += 1;
  87. }
  88. }
  89. double res = s * k / n;
  90. Console.WriteLine("Результат расчёта задачи 1:\n" + res);
  91. }
  92. public void FindingAreaMonteCarlo2()
  93. {
  94. int n = 1000000;
  95. int k = 0;
  96. double x = 0;
  97. double y = 0;
  98. double height = 8;
  99. double weight = 7;
  100. double s = height * weight;
  101. Random random = new Random();
  102. for (int i = 1; i <= n; i++)
  103. {
  104. x = random.NextDouble() * weight;
  105. y = random.NextDouble() * height;
  106. if (y < (x*(8 - x))/2 && y > x/2)
  107. {
  108. k += 1;
  109. }
  110. }
  111. double res = s * k / n;
  112. Console.WriteLine("Результат расчёта задачи 2:\n" + res);
  113. }
  114. public void FindingAreaMonteCarlo3()
  115. {
  116. int n = 1000000;
  117. int k = 0;
  118. double x = 0;
  119. double y = 0;
  120. double height = 6;
  121. double weight = 12;
  122. double s = height * weight;
  123. Random random = new Random();
  124. for (int i = 1; i <= n; i++)
  125. {
  126. x = random.NextDouble() * weight;
  127. y = random.NextDouble() * height;
  128. if (y <= 6 && y > Math.Pow(x - 6, 2)/6)
  129. {
  130. k += 1;
  131. }
  132. }
  133. double res = s * k / n;
  134. Console.WriteLine("Результат расчёта задачи 3:\n" + res);
  135. }
  136. public void FindingAreaMonteCarlo4()
  137. {
  138. int n = 1000000;
  139. int k = 0;
  140. double x = 0;
  141. double y = 0;
  142. double height = 4;
  143. double weight = 10;
  144. double s = height * weight;
  145. Random random = new Random();
  146. for (int i = 1; i <= n; i++)
  147. {
  148. x = random.NextDouble() * weight;
  149. y = random.NextDouble() * height;
  150. if (y > x/5 && y < (x*(12 - x))/9)
  151. {
  152. k += 1;
  153. }
  154. }
  155. double res = s * k / n;
  156. Console.WriteLine("Результат расчёта задачи 4:\n" + res);
  157. }
  158. public void FindingAreaMonteCarlo5()
  159. {
  160. int n = 1000000;
  161. int k = 0;
  162. double x = 0;
  163. double y = 0;
  164. double height = 4;
  165. double weight = 8;
  166. double s = height * weight;
  167. Random random = new Random();
  168. for (int i = 1; i <= n; i++)
  169. {
  170. x = random.NextDouble() * weight;
  171. y = random.NextDouble() * height;
  172. if (y > (8-x)/8 && y < (x*(8 - x))/4)
  173. {
  174. k += 1;
  175. }
  176. }
  177. double res = s * k / n;
  178. Console.WriteLine("Результат расчёта задачи 5:\n" + res);
  179. }
  180. public void FindingAreaMonteCarlo6()
  181. {
  182. int n = 1000000;
  183. int k = 0;
  184. double x = 0;
  185. double y = 0;
  186. double height = 1;
  187. double weight = 2.8;
  188. double s = height * weight;
  189. Random random = new Random();
  190. for (int i = 1; i <= n; i++)
  191. {
  192. x = random.NextDouble() * weight;
  193. y = random.NextDouble() * height;
  194. if (y < Math.Sin(x) && y > Math.Pow(x - 2, 2)/2)
  195. {
  196. k += 1;
  197. }
  198. }
  199. double res = s * k / n;
  200. Console.WriteLine("Результат расчёта задачи 6:\n" + res);
  201. }
  202. }
  203. }