-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (49 loc) · 1.37 KB
/
main.cpp
File metadata and controls
61 lines (49 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <queue>
#include <random>
#include <future>
int amount_of_threads;
int amount_of_calculations;
using namespace std;
int thread_function(int thread_number)
{
mt19937 generator ( time( NULL ) + thread_number*100);
// using thread_number as additional source of entropy
uniform_real_distribution<> distribution(-1,1);
int inside_circle=0;
for(int foo = 0 ; foo < amount_of_calculations; foo++)
{
long double x = distribution(generator);
long double y = distribution(generator);
long double r = sqrt(x*x+y*y);
if(r<=1)
{
inside_circle++;
}
}
return inside_circle;
}
int main(int argc, char **argv)
{
if(argc==1){
amount_of_threads = 2;
amount_of_calculations = 200000;
}else{
amount_of_threads = atoi(argv[1]);
amount_of_calculations = atoi(argv[2]);
}
//Calculating PI using monte carlo method
queue<future<int>> thread_list;
for (int foo = 0; foo < amount_of_threads; foo++)
{
thread_list.push( async(launch::async,thread_function, foo));
}
int inside_circle = 0;
while(!thread_list.empty())
{
inside_circle += thread_list.front().get();
thread_list.pop();
}
double average = inside_circle/amount_of_threads;
return average/amount_of_calculations*4.0*100000;
}