-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonte-carlo.cpp
46 lines (40 loc) · 968 Bytes
/
monte-carlo.cpp
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
#include <iostream>
#include <thread>
#include <stdlib.h>
#include <vector>
#include <atomic>
unsigned long n, m;
std::atomic<long long> total;
std::atomic<long long> hit;
void thread_func(unsigned int id)
{
double x, y;
long long sum = 0, cnt = 0;
for (unsigned i = 0; i < m; ++i) {
x = (double)rand_r(&id) / (double)RAND_MAX;
y = (double)rand_r(&id) / (double)RAND_MAX;
if (x * x + y * y <= 1) {
++cnt;
}
++sum;
}
total += sum;
hit += cnt;
}
int main (int argc, char* argv[])
{
n = strtoul(argv[1], NULL, 10);
m = strtoul(argv[2], NULL, 10);
total.store(0);
hit.store(0);
std::vector <std::thread> threads(n);
for (unsigned i = 0; i < n; ++i) {
threads[i] = std::thread(thread_func, i);
}
for (unsigned i = 0; i < n; ++i) {
threads[i].join();
}
double pi = (double)hit / total * 4;
printf("%0.5f\n", pi);
return 0;
}