fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int max_opportunities = 8; // Total number of chances to flip.
  6. float flip_chance = 0.2; // Probability of flipping each opportunity.
  7. float probability_true = 1.0; // Starting probability of truth.
  8. // 1.0 is "definitely true" and 0.0 is
  9. // "definitely false", but you can extend this
  10. // to situations where the initial value is not
  11. // certain (say, 0.8 = 80% probably true) and
  12. // it will work just as well.
  13. for (int opportunities = 0; opportunities < max_opportunities; ++opportunities)
  14. {
  15. probability_true = probability_true * (1 - flip_chance) +
  16. (1 - probability_true) * flip_chance;
  17. }
  18.  
  19. cout << probability_true << endl;
  20. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
0.508398