MonteP.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Monte_Carlo_P
  7. {
  8. internal class MonteP
  9. {
  10. public void Method_Monte_Karlo(double k)
  11. {
  12. double n = 0;
  13. Console.WriteLine("Введите длину и ширину");
  14. double dlina = Convert.ToDouble(Console.ReadLine());
  15. double shirina = Convert.ToDouble(Console.ReadLine());
  16. double S0 = dlina * shirina;
  17. for (int i = 0; i < k; i++)
  18. {
  19. Random rand = new Random();
  20. double x = rand.NextDouble()*dlina;
  21. double y = rand.NextDouble()*shirina;
  22. if (Math.Pow((x - 1), 2) + Math.Pow((y - 1), 2) <= 1) n++;
  23. }
  24. double S = S0 * n / k;
  25. Console.WriteLine(S);
  26. }
  27. public void Primer0(double k)
  28. {
  29. double n = 0;
  30. double dlina = 8.0;
  31. double shirina = 5.0;
  32. double S0 = dlina * shirina;
  33. for (int i = 0; i < k; i++)
  34. {
  35. Random rand = new Random();
  36. double x = rand.NextDouble() * dlina;
  37. double y = rand.NextDouble() * shirina;
  38. if ((x*(10-x))/5 >= y && (x/3) <= y) n++;
  39. }
  40. double S = S0 * n / k;
  41. Console.WriteLine(S);
  42. }
  43. public void Primer1(double k)
  44. {
  45. double n = 0;
  46. double dlina = 15;
  47. double shirina = 5;
  48. double S0 = dlina * shirina;
  49. for (int i = 0; i < k; i++)
  50. {
  51. Random rand = new Random();
  52. double x = (rand.NextDouble() * dlina) - shirina;
  53. double y = rand.NextDouble();
  54. if (Math.Sin(x) >= y && y>=0) n++;
  55. }
  56. double S = 20 * n / k;
  57. Console.WriteLine(S);
  58. }
  59. public void Primer2(double k)
  60. {
  61. double n = 0;
  62. double dlina = 7;
  63. double shirina = 8;
  64. double S0 = dlina * shirina;
  65. for (int i = 0; i < k; i++)
  66. {
  67. Random rand = new Random();
  68. double x = rand.NextDouble() * dlina;
  69. double y = rand.NextDouble() * shirina;
  70. if ((x*(8-x))/2 >= y && y>= x/2) n++;
  71. }
  72. double S = S0 * n / k;
  73. Console.WriteLine(S);
  74. }
  75. public void Primer3(double k)
  76. {
  77. double n = 0;
  78. double dlina = 12;
  79. double shirina = 6;
  80. double S0 = dlina * shirina;
  81. for (int i = 0; i < k; i++)
  82. {
  83. Random rand = new Random();
  84. double x = rand.NextDouble() * dlina;
  85. double y = rand.NextDouble() * shirina;
  86. if (Math.Pow((x-6),2)/6 <= y && y<=6) n++;
  87. }
  88. double S = S0 * n / k;
  89. Console.WriteLine(S);
  90. }
  91. public void Primer4(double k)
  92. {
  93. double n = 0;
  94. double dlina = 10;
  95. double shirina = 4;
  96. double S0 = dlina * shirina;
  97. for (int i = 0; i < k; i++)
  98. {
  99. Random rand = new Random();
  100. double x = rand.NextDouble() * dlina;
  101. double y = rand.NextDouble() * shirina;
  102. if ((x*(12-x))/9 >= y && y>= x/5) n++;
  103. }
  104. double S = S0 * n / k;
  105. Console.WriteLine(S);
  106. }
  107. public void Primer5(double k)
  108. {
  109. double n = 0;
  110. double dlina = 8;
  111. double shirina = 4;
  112. double S0 = dlina * shirina;
  113. for (int i = 0; i < k; i++)
  114. {
  115. Random rand = new Random();
  116. double x = rand.NextDouble() * dlina;
  117. double y = rand.NextDouble() * shirina;
  118. if ((x * (8 - x)) / 4 >= y && y >= (8-x) / 8) n++;
  119. }
  120. double S = S0 * n / k;
  121. Console.WriteLine(S);
  122. }
  123. public void Primer6(double k)
  124. {
  125. double n = 0;
  126. double dlina = 2.9;
  127. double shirina = 0.99;
  128. double S0 = dlina * shirina;
  129. for (int i = 0; i < k; i++)
  130. {
  131. Random rand = new Random();
  132. double x = rand.NextDouble() * dlina;
  133. double y = rand.NextDouble() * shirina;
  134. if (Math.Sin(x) >= y && y>= Math.Pow((x-2),2)/2) n++;
  135. }
  136. double S = S0 * n / k;
  137. Console.WriteLine(S);
  138. }
  139. public void Choise()
  140. {
  141. Console.WriteLine("Введите k");
  142. double k = Convert.ToDouble(Console.ReadLine());
  143. Console.WriteLine("Расчет числа ПИ");
  144. Method_Monte_Karlo(k);
  145. Console.WriteLine("Расчет примера 0");
  146. Primer0(k);
  147. Console.WriteLine("Расчет примера 1");
  148. Primer1(k);
  149. Console.WriteLine("Расчет примера 2");
  150. Primer2(k);
  151. Console.WriteLine("Расчет примера 3");
  152. Primer3(k);
  153. Console.WriteLine("Расчет примера 4");
  154. Primer4(k);
  155. Console.WriteLine("Расчет примера 5");
  156. Primer5(k);
  157. Console.WriteLine("Расчет примера 6");
  158. Primer6(k);
  159. }
  160. }
  161. }