Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <chrono>
- using namespace std::chrono;
- double pi(int N)
- {
- srand(time(nullptr));
- int M = 0;
- double x = -1, y = -1;
- for (int i = 0; i < N; i++)
- {
- x = (rand() / 32767.0 * 2) - 1;
- y = (rand() / 32767.0 * 2) - 1;
- if (std::pow(x, 2) + std::pow(y, 2) <= 1)
- M++;
- }
- return M * 4.0 / N;
- }
- int main()
- {
- double N = 0;
- printf_s("count: ");
- scanf_s("%lf", &N);
- auto s = high_resolution_clock::now();
- auto p = pi(N);
- auto t = duration_cast<duration<double, std::ratio<1>>> (high_resolution_clock::now() - s).count();
- printf_s("pi: %lf; time: %lf s\n", p, t);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement