fork download
  1. #include <iostream>
  2. #include <random>
  3. #include <vector>
  4.  
  5. int main()
  6. {
  7.  
  8. double lower_limit = -15000;
  9. double upper_limit = 15000;
  10. double range = upper_limit - lower_limit ;
  11.  
  12. std::mt19937 rng((std::random_device())());
  13. std::uniform_real_distribution<double> dist(lower_limit, upper_limit);
  14.  
  15. double highest_result = lower_limit -1;
  16. double lowest_result = upper_limit + 1;
  17.  
  18. const unsigned samples = 1000000;
  19. std::vector<unsigned> block_count(5);
  20.  
  21. for (unsigned i = 0; i < samples; ++i)
  22. {
  23. double value = dist(rng);
  24.  
  25. if (value < lowest_result)
  26. lowest_result = value;
  27.  
  28. if (value > highest_result)
  29. highest_result = value;
  30.  
  31. for (unsigned i = 0; i < 5; ++i)
  32. {
  33. if (value - lower_limit < (range / 5.0) * (i + 1))
  34. {
  35. ++block_count[i];
  36. break;
  37. }
  38. }
  39. }
  40.  
  41. std::cout << "range limit, " << lower_limit << " to " << upper_limit << '\n';
  42. std::cout << "highest result, " << highest_result << '\n';
  43. std::cout << "lowest result, " << lowest_result << '\n';
  44. std::cout << "breakdown of result range by fifths for one run through\n";
  45.  
  46. for (unsigned i = 0; i < 5; ++i)
  47. std::cout << "to " << ((i + 1) / 5.0)*100.0 << "%: " << block_count[i] << " results\n";
  48. }
Success #stdin #stdout 0.09s 3276KB
stdin
Standard input is empty
stdout
range limit, -15000 to 15000
highest result, 15000
lowest result, -15000
breakdown of result range by fifths for one run through
to 20%: 199578 results
to 40%: 199751 results
to 60%: 200452 results
to 80%: 200351 results
to 100%: 199868 results