Skip to content

Instantly share code, notes, and snippets.

@KonradIT
Last active May 13, 2017 14:17
Show Gist options
  • Save KonradIT/ff993a1bf8cf33fc0eb2de7cb278921e to your computer and use it in GitHub Desktop.
Save KonradIT/ff993a1bf8cf33fc0eb2de7cb278921e to your computer and use it in GitHub Desktop.
g++ stdvpi.cpp -o stdvpi && ./stdvpi
#include "stdio.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <numeric>
using namespace std;
int random1 = 1000;
int psize = 1000000;
std::vector< int > piarray;
unsigned int n = 0;
static void picalc(){
unsigned int i = 0;
unsigned int j = 0;
while(j <= random1){
float x = rand();
float y = rand();
//cout << x;
//cout << y;
float distancesquared = pow(x, 2) + pow(y, 2);
if(distancesquared <= pow(1,2)){
i = i + 1;
};
j = j + 1;
};
int pi = 4*(i/(j-1));
piarray.push_back(pi);
};
int main(){
cout << "Enter number of random entries: \n";
cin >> random1;
cout << "Enter number of entries in array: \n";
cin >> psize;
while(piarray.size() < psize){
picalc();
};
float pimean = 1.0 * std::accumulate(piarray.begin(), piarray.end(), 0LL) / piarray.size();
cout << pimean;
printf("\nterminated");
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment