Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Windows.Forms;
- using System.Diagnostics;
- namespace MonteCarloPi
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- // Po kliknięciu w guzik Policz, zostaje obliczone PI metodą Monte Carlo
- // Opis działania guzika
- private void calculateBtn_Click(object sender, EventArgs e)
- {
- string sData = losowaniaBox.Text;
- int threadNr = int.Parse(thread_nr.Text);
- List<Thread> threads = new List<Thread>();
- for(int i = 0; i < threadNr ; i++)
- {
- Thread oThread = new Thread(() => { getPI(sData); });
- threads.Add(oThread);
- }
- foreach (Thread thread in threads)
- {
- thread.Start();
- }
- //getPI(sData);
- //oThread.Start();
- //oThread.Join();
- }
- public void getPI(object oData)
- {
- Int64 n = Convert.ToInt64(oData);
- int procesory = 0;
- int trafienie = 0;
- Random r = new Random();
- Stopwatch stopWatch1 = Stopwatch.StartNew();
- for (int i = 0; i < n; i++)
- {
- if (Math.Pow(r.NextDouble(), 2) + Math.Pow(r.NextDouble(), 2) <= 1)
- {
- trafienie++;
- }
- }
- double aproksymacja = 4.0 * trafienie / n;
- double bezwzgledna = Math.Abs(Math.PI - aproksymacja);
- stopWatch1.Stop();
- thread_time.Text = stopWatch1.Elapsed.TotalSeconds.ToString();
- //wypisywanie do poszczególnych textboxów
- resultBox.Text = trafienie.ToString();
- aproksymacjaBox.Text = aproksymacja.ToString();
- errorBox.Text = bezwzgledna.ToString();
- procesory = Environment.ProcessorCount;
- processor_nr.Text = procesory.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement