// probability.cpp // // Abdul Khan // 01/30/02 // // Find the probability that a given even will occur x times in N trials // Test case returned Prob(100,30)= 2.31707e-05 and Prob(1000,400)= 4.63391e-11 //#include "stdafx.h" #include #include double choose(const double N, const double x) { double c = 0; for (int i=1;i<=N;i++) { c += log(i); if (i <= x ) c -= log(i); if (i <= N-x) c -= log(i); } return pow(exp(1),c); }; int main(int argc, char* argv[]) { double N,x; double p = 0.5; do { cout << "This program will calculate the probability that an N evenly chanced\n"; cout << "events will result in one of the outcomes exactly x times\n"; cout << "Please enter N "; cin >> N; cout << "Please enter x "; cin >> x; cout << "The chances of that are " << (choose(N,x)*pow(p,x)*(pow((1-p),(N-x)))) << endl; } while (x && N); return 0; };