Denshiro

change simulation

Jun 12th, 2017
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <windows.h>
  7.  
  8. #define FLT_EPSILON     1.192092896e-07F
  9. #define RAND_MAX 0x7fff
  10.  
  11. using namespace std;
  12.  
  13. int number(int from, int to)
  14. {
  15.     if (from > to)
  16.     {
  17.         int tmp = from;
  18.  
  19.         from = to;
  20.         to = tmp;
  21.     }
  22.  
  23.     int returnValue = 0;
  24.  
  25.     if ((to - from + 1) != 0)
  26.     {
  27.         returnValue = ((rand() % (to - from + 1)) + from);
  28.     }
  29.  
  30.     return returnValue;
  31. }
  32.  
  33. int MINMAX(int min, int value, int max)
  34. {
  35.     register int tv;
  36.  
  37.     tv = (min > value ? min : value);
  38.     return (max < tv) ? max : tv;
  39. }
  40.  
  41. double uniform_random(double a, double b)
  42. {
  43.     return rand() / (RAND_MAX + 1.f) * (b - a) + a;
  44. }
  45.  
  46. float gauss_random(float avg, float sigma)
  47. {
  48.     static bool haveNextGaussian = false;
  49.     static float nextGaussian = 0.0f;
  50.  
  51.     if (haveNextGaussian)
  52.     {
  53.         haveNextGaussian = false;
  54.         return nextGaussian * sigma + avg;
  55.     }
  56.     else
  57.     {
  58.         double v1, v2, s;
  59.         do {
  60.             //v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
  61.             //v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
  62.             v1 = uniform_random(-1.f, 1.f);
  63.             v2 = uniform_random(-1.f, 1.f);
  64.             s = v1 * v1 + v2 * v2;
  65.         } while (s >= 1.f || fabs(s) < FLT_EPSILON);
  66.         double multiplier = sqrtf(-2 * logf(s) / s);
  67.         nextGaussian = v2 * multiplier;
  68.         haveNextGaussian = true;
  69.         return v1 * multiplier * sigma + avg;
  70.     }
  71. }
  72.  
  73. int main()
  74. {
  75.     int iSkillBonus, iNormalHitBonus;
  76.     unsigned long long counter = 0;
  77.     srand(time(NULL));
  78.     for (;;)
  79.     {
  80.         iSkillBonus = MINMAX(-30, (int)(gauss_random(0, 5) + 0.5f), 30);
  81.         if (abs(iSkillBonus) <= 20)
  82.             iNormalHitBonus = -2 * iSkillBonus + abs(number(-8, 8) + number(-8, 8)) + number(1, 4);
  83.         else
  84.             iNormalHitBonus = -2 * iSkillBonus + number(1, 5);
  85.    
  86.         counter += 1;
  87.         if (iNormalHitBonus >= 55) ////if normal hit bonus is 55 or higher, the loop will stop
  88.             break;
  89.    
  90.         Sleep(1); //how long you will wait between draws
  91.    
  92.  
  93.         //you can delete this if you want to make it faster
  94.         cout << "normal hit bonus: " << iNormalHitBonus << endl;
  95.         cout << "skill bonus: " << iSkillBonus << endl;
  96.         //
  97.  
  98.     }
  99.  
  100.     cout << "you've used " << counter << " changes" << endl;
  101.  
  102.     system("pause");
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment