fork download
  1. #include <stdlib.h>
  2. #include <cmath>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int rangeRandomAlg1 (int min, int max)
  7. {
  8. static const double s_invRandMax = 1.0/((double)RAND_MAX + 1.0);
  9. return min + (int)(((double)(max + 1 - min))*rand()*s_invRandMax);
  10. }
  11.  
  12. int rangeRandomAlg2 (int min, int max)
  13. {
  14. int n = max - min + 1;
  15. int remainder = RAND_MAX % n;
  16. int x, output;
  17. do
  18. {
  19. x = rand();
  20. output = x % n;
  21. } while (x > RAND_MAX - remainder);
  22. return min + output;
  23. }
  24.  
  25. int main ()
  26. {
  27. static const int s_numBuckets = 10; // the number of "buckets"
  28. static const int s_numBalls = 100; // the number of "balls"
  29. int buckets[s_numBuckets];
  30. double idealProbPerBucket = 1.0/s_numBuckets;
  31. double maxDevFromIdealProb;
  32.  
  33. // Testing algorithm 1
  34. cout << "Testing with floating-point mapping:" << endl;
  35. for (int i = 0; i < s_numBuckets; i++)
  36. {
  37. buckets[i] = 0;
  38. }
  39. srand(0);
  40. for (int i = 0; i < s_numBalls; i++)
  41. {
  42. buckets[rangeRandomAlg1(0, s_numBuckets - 1)]++;
  43. }
  44. maxDevFromIdealProb = -1.0;
  45. for (int i = 0; i < s_numBuckets; i++)
  46. {
  47. double prob = ((double)buckets[i])/s_numBalls;
  48. cout << "Probability for bucket #" << i << ": " << prob << endl;
  49.  
  50. double devFromIdealProb = fabs(prob - idealProbPerBucket);
  51. if ( devFromIdealProb > maxDevFromIdealProb )
  52. {
  53. maxDevFromIdealProb = devFromIdealProb;
  54. }
  55. }
  56. cout << ">>> Maximum deviation from the ideal probability: " << maxDevFromIdealProb << endl;
  57. cout << endl;
  58.  
  59. // Testing algorithm 2
  60. cout << "Testing with Java's algorithm:" << endl;
  61. for (int i = 0; i < s_numBuckets; i++)
  62. {
  63. buckets[i] = 0;
  64. }
  65. srand(0);
  66. for (int i = 0; i < s_numBalls; i++)
  67. {
  68. buckets[rangeRandomAlg2(0, s_numBuckets - 1)]++;
  69. }
  70. maxDevFromIdealProb = -1.0;
  71. for (int i = 0; i < s_numBuckets; i++)
  72. {
  73. double prob = ((double)buckets[i])/s_numBalls;
  74. cout << "Probability for bucket #" << i << ": " << prob << endl;
  75.  
  76. double devFromIdealProb = fabs(prob - idealProbPerBucket);
  77. if ( devFromIdealProb > maxDevFromIdealProb )
  78. {
  79. maxDevFromIdealProb = devFromIdealProb;
  80. }
  81. }
  82. cout << ">>> Maximum deviation from the ideal probability: " << maxDevFromIdealProb << endl;
  83.  
  84. return 0;
  85. }
Success #stdin #stdout 0.01s 2684KB
stdin
Standard input is empty
stdout
Testing with floating-point mapping:
Probability for bucket #0: 0.07
Probability for bucket #1: 0.08
Probability for bucket #2: 0.1
Probability for bucket #3: 0.1
Probability for bucket #4: 0.08
Probability for bucket #5: 0.1
Probability for bucket #6: 0.12
Probability for bucket #7: 0.09
Probability for bucket #8: 0.12
Probability for bucket #9: 0.14
>>> Maximum deviation from the ideal probability: 0.04

Testing with Java's algorithm:
Probability for bucket #0: 0.08
Probability for bucket #1: 0.07
Probability for bucket #2: 0.12
Probability for bucket #3: 0.11
Probability for bucket #4: 0.1
Probability for bucket #5: 0.09
Probability for bucket #6: 0.15
Probability for bucket #7: 0.1
Probability for bucket #8: 0.07
Probability for bucket #9: 0.11
>>> Maximum deviation from the ideal probability: 0.05