CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MonteCarlo
{
public class Program
{
private static Random r = new Random();
private static double funkcja(double x, double y)
{
return Math.Sin(x) * Math.Sin(y);
}
private static double losowyPunkt(double a, double b)
{
return r.NextDouble() * (b - a) + a;
}
public static void Main(string[] args)
{
int n, calka = 0;
double pa, pb;
Console.Write("Podaj N:");
n = Convert.ToInt32(Console.ReadLine());
for(int i=0; i<n; i++)
{
pa = losowyPunkt(-10, 10);
pb = losowyPunkt(-10, 10);
if(funkcja(pa, pb) < (1/4))
{
calka++;
}
}
Console.WriteLine("Calka = " + calka);
Console.ReadLine();
}
}
}