123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MonteCarlo
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Decision decision = new Decision();
- decision.FindingPi();
- decision.FindingAreaMonteCarloPrimer();
- decision.FindingAreaMonteCarlo1();
- decision.FindingAreaMonteCarlo2();
- decision.FindingAreaMonteCarlo3();
- decision.FindingAreaMonteCarlo4();
- decision.FindingAreaMonteCarlo5();
- decision.FindingAreaMonteCarlo6();
- Console.ReadKey();
- }
- }
- public class Decision
- {
- public void FindingPi()
- {
- int n = 100000;
- int k = 0;
- double x = 0;
- double y = 0;
- double height = 2;
- double weight = 2;
- double s = height * weight;
- Random random = new Random();
- for (int i = 1; i <= n; i++)
- {
- x = random.NextDouble();
- y = random.NextDouble();
- if (Math.Pow(x - 1,2) + Math.Pow(y - 1,2) <= 1)
- {
- k += 1;
- }
- }
- double Pi = s * k / n;
- Console.WriteLine("Результат расчёта Pi:\n" + Pi);
- }
- public void FindingAreaMonteCarloPrimer()
- {
- int n = 1000000;
- int k = 0;
- double x = 0;
- double y = 0;
- double height = 5;
- double weight = 8.5;
- double s = height * weight;
- Random random = new Random();
- for (int i = 1; i <= n; i++)
- {
- x = random.NextDouble() * weight;
- y = random.NextDouble() * height;
- if (y < (x*(10 - x))/5 && y > x/3)
- {
- k += 1;
- }
- }
- double res = s * k / n;
- Console.WriteLine("Результат расчёта примера:\n" + res);
- }
- public void FindingAreaMonteCarlo1()
- {
- int n = 1000000;
- int k = 0;
- double x = 0;
- double y = 0;
- double height = 1;
- double weight = 20;
- double s = height * weight;
- Random random = new Random();
- for (int i = 1; i <= n; i++)
- {
- x = random.NextDouble() * weight - 5;
- y = random.NextDouble() * height;
- if (y < Math.Sin(x) && y > 0)
- {
- k += 1;
- }
- }
- double res = s * k / n;
- Console.WriteLine("Результат расчёта задачи 1:\n" + res);
- }
- public void FindingAreaMonteCarlo2()
- {
- int n = 1000000;
- int k = 0;
- double x = 0;
- double y = 0;
- double height = 8;
- double weight = 7;
- double s = height * weight;
- Random random = new Random();
- for (int i = 1; i <= n; i++)
- {
- x = random.NextDouble() * weight;
- y = random.NextDouble() * height;
- if (y < (x*(8 - x))/2 && y > x/2)
- {
- k += 1;
- }
- }
- double res = s * k / n;
- Console.WriteLine("Результат расчёта задачи 2:\n" + res);
- }
- public void FindingAreaMonteCarlo3()
- {
- int n = 1000000;
- int k = 0;
- double x = 0;
- double y = 0;
- double height = 6;
- double weight = 12;
- double s = height * weight;
- Random random = new Random();
- for (int i = 1; i <= n; i++)
- {
- x = random.NextDouble() * weight;
- y = random.NextDouble() * height;
- if (y <= 6 && y > Math.Pow(x - 6, 2)/6)
- {
- k += 1;
- }
- }
- double res = s * k / n;
- Console.WriteLine("Результат расчёта задачи 3:\n" + res);
- }
- public void FindingAreaMonteCarlo4()
- {
- int n = 1000000;
- int k = 0;
- double x = 0;
- double y = 0;
- double height = 4;
- double weight = 10;
- double s = height * weight;
- Random random = new Random();
- for (int i = 1; i <= n; i++)
- {
- x = random.NextDouble() * weight;
- y = random.NextDouble() * height;
- if (y > x/5 && y < (x*(12 - x))/9)
- {
- k += 1;
- }
- }
- double res = s * k / n;
- Console.WriteLine("Результат расчёта задачи 4:\n" + res);
- }
- public void FindingAreaMonteCarlo5()
- {
- int n = 1000000;
- int k = 0;
- double x = 0;
- double y = 0;
- double height = 4;
- double weight = 8;
- double s = height * weight;
- Random random = new Random();
- for (int i = 1; i <= n; i++)
- {
- x = random.NextDouble() * weight;
- y = random.NextDouble() * height;
- if (y > (8-x)/8 && y < (x*(8 - x))/4)
- {
- k += 1;
- }
- }
- double res = s * k / n;
- Console.WriteLine("Результат расчёта задачи 5:\n" + res);
- }
- public void FindingAreaMonteCarlo6()
- {
- int n = 1000000;
- int k = 0;
- double x = 0;
- double y = 0;
- double height = 1;
- double weight = 2.8;
- double s = height * weight;
- Random random = new Random();
- for (int i = 1; i <= n; i++)
- {
- x = random.NextDouble() * weight;
- y = random.NextDouble() * height;
- if (y < Math.Sin(x) && y > Math.Pow(x - 2, 2)/2)
- {
- k += 1;
- }
- }
- double res = s * k / n;
- Console.WriteLine("Результат расчёта задачи 6:\n" + res);
- }
- }
- }
|