30_gauss.cpp 761 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include <boost/random.hpp>
  2. #include <cmath>
  3. #include <iostream>
  4. #define mean 0.0
  5. #define sigma 1.0
  6. #define EPS 0.01
  7. #define SAMPLES 10000000
  8. using namespace boost::random;
  9. using namespace std;
  10. int main()
  11. {
  12. mt19937 rng(static_cast<unsigned int>(time(0)));
  13. normal_distribution<double> dist(mean, sigma);
  14. cout << "Generating numbers..." << endl;
  15. int meanpoints = 0;
  16. int epoints = 0;
  17. for (int i = 0; i < SAMPLES; i++)
  18. {
  19. double rnd = dist(rng);
  20. if (fabs(rnd - mean) < EPS)
  21. meanpoints++;
  22. if ((fabs(rnd - mean - sqrt(2) * sigma) < EPS) ||
  23. (fabs(rnd - mean + sqrt(2) * sigma) < EPS))
  24. epoints++;
  25. }
  26. cout << "Expected f(mean)/f(mean+sqrt(2)*sigma): " << M_E << endl;
  27. cout << "Computed: " << meanpoints * 2.0 / epoints << endl;
  28. return 0;
  29. }