fork download
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. int aCount = 0, bCount = 0, cCount = 0;
  5.  
  6. void a() { ++aCount; }
  7. void b() { ++bCount; }
  8. void c() { ++cCount; }
  9.  
  10. float aChance = 0.3f;
  11. float bChance = 0.1f;
  12. float cChance = 0.6f;
  13.  
  14. const size_t n = 3;
  15. float chance[n] = {aChance, aChance + bChance, 1};
  16. void (*f[n])() = {a, b, c};
  17.  
  18. int main()
  19. {
  20. for (unsigned t=0; t<1000000; ++t)
  21. {
  22. float x = rand() / (float)RAND_MAX;
  23.  
  24. for (unsigned q=0; q<n; ++q)
  25. if(x <= chance[q])
  26. {
  27. f[q]();
  28. break;
  29. }
  30. }
  31.  
  32. printf("%d %d %d\n%d", aCount, bCount, cCount, aCount+bCount+cCount);
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0.02s 3468KB
stdin
Standard input is empty
stdout
299648 100144 600208
1000000