Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using System.Diagnostics;
  12.  
  13. namespace MonteCarloPi
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.        
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.  
  23.         }
  24.  
  25.         // Po kliknięciu w guzik Policz, zostaje obliczone PI metodą Monte Carlo
  26.         // Opis działania guzika
  27.  
  28.         private void calculateBtn_Click(object sender, EventArgs e)
  29.         {
  30.            
  31.             string sData = losowaniaBox.Text;
  32.             int threadNr = int.Parse(thread_nr.Text);
  33.             List<Thread> threads = new List<Thread>();
  34.            
  35.            
  36.             for(int i = 0; i < threadNr ; i++)
  37.             {
  38.                 Thread oThread = new Thread(() => { getPI(sData); });
  39.                 threads.Add(oThread);
  40.             }
  41.            
  42.             foreach (Thread thread in threads)
  43.             {
  44.                 thread.Start();
  45.             }
  46.  
  47.             //getPI(sData);
  48.             //oThread.Start();
  49.             //oThread.Join();
  50.            
  51.         }
  52.  
  53.         public void getPI(object oData)
  54.         {
  55.             Int64 n = Convert.ToInt64(oData);
  56.             int procesory = 0;
  57.             int trafienie = 0;
  58.             Random r = new Random();
  59.  
  60.             Stopwatch stopWatch1 = Stopwatch.StartNew();
  61.             for (int i = 0; i < n; i++)
  62.             {
  63.                 if (Math.Pow(r.NextDouble(), 2) + Math.Pow(r.NextDouble(), 2) <= 1)
  64.                 {
  65.                     trafienie++;
  66.                 }
  67.             }
  68.  
  69.             double aproksymacja = 4.0 * trafienie / n;
  70.             double bezwzgledna = Math.Abs(Math.PI - aproksymacja);
  71.  
  72.             stopWatch1.Stop();
  73.             thread_time.Text = stopWatch1.Elapsed.TotalSeconds.ToString();
  74.  
  75.             //wypisywanie do poszczególnych textboxów
  76.  
  77.             resultBox.Text = trafienie.ToString();
  78.             aproksymacjaBox.Text = aproksymacja.ToString();
  79.             errorBox.Text = bezwzgledna.ToString();
  80.  
  81.             procesory = Environment.ProcessorCount;
  82.             processor_nr.Text = procesory.ToString();
  83.  
  84.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement