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

Testing with Java's algorithm:
Probability for bucket #0: 0.100081
Probability for bucket #1: 0.100567
Probability for bucket #2: 0.099835
Probability for bucket #3: 0.099904
Probability for bucket #4: 0.100061
Probability for bucket #5: 0.099398
Probability for bucket #6: 0.099663
Probability for bucket #7: 0.10052
Probability for bucket #8: 0.099906
Probability for bucket #9: 0.100065
>>> Maximum deviation from the ideal probability: 0.000602