Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int N = 1000000000;
- int main()
- {
- srand(time(nullptr));
- double x, y; // współrzędne losowanego punktu
- int n = 0;
- // pętla losująca:
- for(int i = 0; i < N; ++i)
- {
- // losowanie współrzędnych:
- x = 1.0 * rand() / RAND_MAX;
- y = 1.0 * rand() / RAND_MAX;
- // sprawdzenie czy losowany punkt należy do ćwiartki okręgu:
- if(x*x + y*y <= 1)
- ++n; // punkt zawiera się w ćwiartce okręgu, więc dodajemy go
- }
- cout << "Liczbe PI oszacowano na " << 4.0 * n / N << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment