Created
March 4, 2017 10:54
-
-
Save anonymous/e09a2d11741aa54ddadb7e83bedc0540 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pieholes.cpp : Defines the entry point for the console application. | |
// | |
#include <random> | |
#include <vector> | |
#include <iostream> | |
#include <math.h> | |
#include <functional> | |
#include <tuple> | |
int main() | |
{ | |
std::random_device random_device; | |
std::mt19937 engine{random_device()}; | |
std::uniform_int_distribution<int> dist(0, 9); | |
auto generate_random_number = [&](int digits) | |
{ | |
double result = 0; | |
for (int i = 0; i < digits; ++i) | |
result = (result * 10) + dist(engine); | |
return result; | |
}; | |
const double pi = 3.14159265359; | |
std::cout.setf(std::ios::fixed, std::ios::floatfield); | |
std::cout.precision(11); | |
for (int n = 0; n <= 9; ++n) | |
{ | |
auto generate_random_coordinate = std::bind(generate_random_number, n); | |
double radius = std::pow(10, n); | |
for (int m = 0; m <= 7; ++m) | |
{ | |
auto generate_random_shot = [&]() | |
{ | |
return std::make_tuple( | |
generate_random_coordinate(), | |
generate_random_coordinate()); | |
}; | |
auto shoot_the_circle = [&]() | |
{ | |
auto shot = generate_random_shot(); | |
double distance = std::sqrt(std::get<0>(shot)*std::get<0>(shot) + std::get<1>(shot)*std::get<1>(shot)); | |
return (distance < radius); | |
}; | |
int hits = 0; | |
int shots = static_cast<int>(std::pow(10, m)); | |
for (int shot = 0; shot < shots; ++shot) | |
{ | |
if (shoot_the_circle()) | |
hits++; | |
} | |
double result = 1.0 * hits / shots * 4; | |
std::cout << std::abs(pi - result) << "\t"; | |
} | |
std::cout << std::endl; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment