fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <map>
  5. #include <random>
  6. #include <cmath>
  7. std::random_device rd;
  8. std::mt19937 gen(rd());
  9. std::normal_distribution<> d(0,1);
  10. double invErr;
  11.  
  12. double f(float skew){
  13. float x = d(gen);
  14. while (x < -1 || x > 1)
  15. x = d(gen);
  16. double c = invErr;
  17. if (x < c)
  18. return (x + 1) * (skew) / (c - (-1));
  19. else
  20. return (x - c) * (1-skew) / (1 - c) + skew;
  21. }
  22. int main()
  23. {
  24.  
  25. // values near the mean are the most likely
  26. // standard deviation affects the dispersion of generated values from the mean
  27. double center = 0.2;
  28. invErr = (center - 0.5) * 2;
  29. std::map<int, int> hist;
  30. float step = 20;
  31. for(int n=0; n<100000; ++n) {
  32. ++hist[std::round(f(center) * step)];
  33. }
  34. for(auto p : hist) {
  35. std::cout << std::fixed << std::setprecision(2) << std::setw(2)
  36. << p.first / step << ' ' << std::string(std::round(p.second/500.0f), '*') << '\n';
  37. }
  38. }
Success #stdin #stdout 0.03s 3420KB
stdin
Standard input is empty
stdout
0.00 ****
0.05 ********
0.10 ********
0.15 *********
0.20 **********
0.25 **********
0.30 ***********
0.35 ***********
0.40 ***********
0.45 ************
0.50 ************
0.55 ************
0.60 ************
0.65 ***********
0.70 ***********
0.75 **********
0.80 **********
0.85 *********
0.90 *********
0.95 ********
1.00 ****