fork(3) download
  1. #include <iostream>
  2. #include <random>
  3. using namespace std;
  4.  
  5. static std::random_device __randomDevice;
  6. static std::mt19937 __randomGen(__randomDevice());
  7. static std::normal_distribution<float> __normalDistribution(0.5, 1);
  8.  
  9. // Get a normally distributed float value in the range [0,1].
  10. inline float GetNormDistrFloat()
  11. {
  12. float val = -1;
  13. do { val = __normalDistribution(__randomGen); }
  14. while(val < 0.0f || val > 1.0f);
  15.  
  16. return val;
  17. }
  18.  
  19. int main() {
  20. int count1=0;
  21. int count2=0;
  22. int count3=0;
  23. int count4=0;
  24. for (int i =0; i< 1000000; i++) {
  25. float val = GetNormDistrFloat();
  26. if (val<0.25){ count1++; continue;}
  27. if (val<0.5){ count2++; continue;}
  28. if (val<0.75){ count3++; continue;}
  29. if (val<1){ count4++; continue;}
  30. }
  31. std::cout<<count1<<", "<<count2<<", "<<count3<<", "<<count4<<std::endl;
  32. return 0;
  33. }
Success #stdin #stdout 0.1s 16072KB
stdin
Standard input is empty
stdout
241395, 258131, 258275, 242199