MonteKarlo.cs 779 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace montekarlo
  7. {
  8. internal class MonteKarlo
  9. {
  10. public static void MonteKarloP()
  11. {
  12. int n = 1000;
  13. int k = 0;
  14. double S;
  15. Random rand = new Random();
  16. for (int i = 1; i < n; i++)
  17. {
  18. double x = rand.NextDouble();
  19. double y = rand.NextDouble();
  20. if (x * x + y * y <= 1)
  21. {
  22. k++;
  23. }
  24. }
  25. S = 4.0 * k / n;
  26. Console.WriteLine("Результат Pi = " + S);
  27. Console.WriteLine("Точно Pi = " + Math.PI);
  28. }
  29. }
  30. }