Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- const unsigned int N{ 1'000'000'000 };
- double szacujPi() {
- unsigned int n{}; // licznik określający ile punktów zawiera się w ćwiartce koła
- for (unsigned int i{}; i < N; ++i) {
- // losowanie punktu (x, y):
- auto x{ 1.0 * rand() / RAND_MAX };
- auto y{ 1.0 * rand() / RAND_MAX };
- // sprawdzamy czy punkt (x, y) zawiera się w ćwiartce koła:
- if (x * x + y * y <= 1) {
- // punkt zawiera się w ćwiartce koła, więc zwiększamy n o 1:
- ++n;
- }
- }
- return 4.0 * n / N;
- }
- int main() {
- srand(time(nullptr));
- std::cout << "Wartosc liczby pi oszacowano na " << szacujPi() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement