Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h" // default
- #include <random> // for random
- #include <math.h> // for sqrt
- #include <iostream> // output
- double radius = 500;
- int random_int(int min, int max) // pesudo random
- {
- std::random_device seeder;
- std::mt19937 engine(seeder());
- std::uniform_int_distribution<int> dist(min, max);
- return dist(engine);
- }
- int main()
- {
- // variables
- int total_dots = 0;
- int dots_in = 0;
- double pi = 0;
- while (1) // loop
- {
- for (int i = 0; i < 10000; i++) // do 10,000 times before refreshing console output
- {
- double point_x = (double)(random_int(radius*-1, radius));
- double point_y = (double)(random_int(radius*-1, radius));
- total_dots++; // add onto total dots (in our invisible drawing)
- if (point_x < 0) // make points positive to deal w/ errors
- point_x *= -1;
- if (point_y < 0)
- point_y *= -1;
- // pythagorean theorem
- double distance = sqrt(point_x * point_x + point_y * point_y);
- if (distance < radius)
- dots_in++;
- }
- pi = 4 * (double(dots_in) / double(total_dots));
- std::cout << pi << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement