fork(2) download
  1. #include <iostream>
  2. #include <random>
  3.  
  4. int main()
  5. {
  6. std::random_device rd;
  7. std::mt19937 gen(rd());
  8. std::uniform_int_distribution<> dis(0, 1);
  9.  
  10. unsigned trials = 0;
  11. unsigned survive = 0;
  12. for(unsigned i = 0; i < 100000; ++i)
  13. {
  14. int clear1 = dis(gen);
  15. int clear2 = dis(gen);
  16. if(clear1 && clear2) //both female, one didn't croak
  17. {
  18. continue;
  19. }
  20. ++trials;
  21. if(clear1 || clear2) //at least one was female
  22. {
  23. ++survive;
  24. }
  25. }
  26. std::cout << survive << std::endl;
  27. std::cout << (survive/1.0/trials)*100.0 << "%" << std::endl;
  28. }
  29.  
Success #stdin #stdout 0.01s 3416KB
stdin
Standard input is empty
stdout
50294
66.9444%