myHelper.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include <cstdlib>
  3. #include <random>
  4. #include <cmath>
  5. #include <math.h>
  6. #include <iostream>
  7. #include <ctime>
  8. #include <string>
  9. #include <vector>
  10. #define M_PI 3.1413927
  11. using namespace std;
  12. class myHelper {
  13. public:
  14. template <typename T>
  15. static T uniformDistribution(T a, T b) {
  16. T max, min;
  17. if (a > b) {
  18. max = a; min = b;
  19. }
  20. else {
  21. max = b; min = a;
  22. }
  23. random_device rd;
  24. mt19937 gen(rd());
  25. uniform_real_distribution<double> distr(min, max);
  26. return (T)distr(gen);
  27. }
  28. template <typename T>
  29. static T normalDistribution(T u, T o) {
  30. srand(time(0));
  31. double u1 = uniformDistribution<double>(0.0, 1.0);
  32. double u2 = uniformDistribution<double>(0.0, 1.0);
  33. double z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
  34. double z1 = sqrt(-2.0 * log(u1)) * sin(2.0 * M_PI * u2);
  35. double res = z0 * o + u;
  36. return (T)res;
  37. }
  38. static int randomInt(int a, int b) {
  39. if (a > b) {
  40. int temp = a;
  41. a = b;
  42. b = temp;
  43. }
  44. return (int)uniformDistribution<int>(a, b);
  45. }
  46. static int getRandomInt(int a, int b) {
  47. return randomInt(a, b);
  48. }
  49. template <typename T>
  50. static T getRandomItemFrom(vector<T> arr) {
  51. int index = randomInt(0, arr.size());
  52. return arr[index];
  53. }
  54. static double randomDouble(double a, double b) {
  55. if (a > b) {
  56. double temp = a;
  57. a = b;
  58. b = temp;
  59. }
  60. return (double)uniformDistribution<double>(a, b);
  61. }
  62. static string randomString(int len = 9) {
  63. const char alphanum[] =
  64. "0123456789"
  65. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  66. "abcdefghijklmnopqrstuvwxyz";
  67. int stringLen = sizeof(alphanum) - 1;
  68. string str;
  69. for (int i = 0; i < len; i++) {
  70. int rnd = randomInt(0, stringLen);
  71. str += alphanum[rnd % stringLen];
  72. }
  73. return str;
  74. }
  75. static int coinFlip() {
  76. return randomInt(0, 2);
  77. }
  78. static int diceRoll() {
  79. return randomInt(1, 7);
  80. }
  81. static int diceRoll(int size) {
  82. return randomInt(1, size + 1);
  83. }
  84. };