Advertisement
Archon

Random

Apr 16th, 2011
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <cstdlib>
  2.  
  3. int random(int min, int max, unsigned rolls = 5, float stretch = 2);
  4.  
  5. //int random(int min, int max, unsigned rolls){
  6. //   unsigned span = max - min + 1;
  7. //   unsigned total = 0;
  8. //   for (unsigned i = 0; i != rolls; ++i)
  9. //      total += rand() % span;
  10. //   return (total + 0.5) / rolls + min;
  11. //}
  12.  
  13. int random(int min, int max, unsigned rolls, float stretch){
  14.    if (rolls == 0)
  15.       return (min + max + 1.5) / 2;
  16.    if (rolls == 1)
  17.       return rand() % (max - min + 1);
  18.  
  19.    int result;
  20.    do{
  21.       unsigned span = max - min + 1;
  22.       unsigned stretchedSpan = span * stretch + .5;
  23.       unsigned total = 0;
  24.       for (unsigned i = 0; i != rolls; ++i)
  25.          total += rand() % stretchedSpan;
  26.       result = 1.0 * total / rolls + .5;
  27.       result -= ((stretchedSpan - span) / 2 - 0.5);
  28.       result += min;
  29.    }while(result < min || result > max);
  30.    return result;
  31. }
  32.  
  33.  
  34. #include <iostream>
  35.  
  36. void testRandom(int min = 1, int max = 20, unsigned rolls = 5, float stretch = 1.5, int data = 800, int multiplier = 10000){
  37.    int span = max - min + 1;
  38.    unsigned a[10000];
  39.    if (span > 10000)
  40.       span = 10000;
  41.    for (int i = 0; i != span; ++i)
  42.       a[i] = 0;
  43.  
  44.    int num = data * multiplier;
  45.    for (int i = 0; i != num; ++i){
  46.       int index = random(min, max, rolls, stretch) - min;
  47.       ++a[index];
  48.    }
  49.    for (int i = min; i != max + 1; ++i){
  50.       if (i < 10)
  51.          std::cout << ' ';
  52.       if (i < 100)
  53.          std::cout << ' ';
  54.       if (i < 1000)
  55.          std::cout << ' ';
  56.       std::cout << i << '|';
  57.       for (int j = 0; j != (a[i-min] + multiplier/2) / multiplier; ++j)
  58.          std::cout << '*';
  59.       std::cout << '\n';
  60.    }
  61.    std::cout << std::endl;
  62. }
  63.  
  64. int main(){
  65.    testRandom(1, 80, 5, 1.5, 1500);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement