fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. const int MAX = 10000;
  5. int Prob (int num)
  6. {
  7. int Cnc = (std::rand() % MAX)+1;
  8. if (Cnc <= num)
  9. return 1;
  10. else
  11. return 0;
  12. }
  13.  
  14. int main()
  15. {
  16. std::srand(std::time(NULL));
  17. double positives = 0 ;
  18. for(int n = 0; n < 10000; ++n)
  19. positives += Prob(5000);
  20. std::cout << "positives/MAX = " << positives / MAX << '\n';
  21. }
  22.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
positives/MAX = 0.5002