fork(5) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int process() {
  5. int previous_result = -1;
  6. int tosses = 0;
  7. while(true) {
  8. int x = rand() % 2;
  9. ++tosses;
  10. if(previous_result == 1 && x == 1) {
  11. return tosses;
  12. }
  13. previous_result = x;
  14. }
  15. }
  16.  
  17. int main() {
  18. const int tries = 8000000;
  19. double sum = 0;
  20. for(int rep = 0; rep < tries; ++rep) {
  21. sum += process();
  22. }
  23. printf("%lf\n", sum / tries); // average value
  24. }
  25.  
  26. // N tries -> precision 1/sqrt(N)
  27. // 10^6 -> precision 0.001
Success #stdin #stdout 0.43s 15232KB
stdin
Standard input is empty
stdout
5.999830