[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppNormalDistribution
The function GetCumulativeDensityNormalDistribution returns the cumulative density at a certain x of a normal distribution with a mean of 0.0 and standard deviation of 0.0. This code was copied from this location.
or in a function:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppNormalDistribution
(C++) Normal distribution
The function GetDensityNormalDistribution returns the density at a certain x of a normal distribution with a certain mean and standard deviation.The function GetCumulativeDensityNormalDistribution returns the cumulative density at a certain x of a normal distribution with a mean of 0.0 and standard deviation of 0.0. This code was copied from this location.
double GetDensityNormalDistribution(const double x, const double mean, const double stddev) { const double firstTerm = 1.0 / (stddev * std::sqrt(2.0 * M_PI)); const double secondTerm = -( (x - mean) * (x - mean) / (2.0 * stddev * stddev) ); const double result = firstTerm * std::exp(secondTerm); return result; } double GetCumulativeDensityNormalDistribution(const double x) { const double c0 = 0.2316419 ; const double c1 = 1.330274429; const double c2 = 1.821255978; const double c3 = 1.781477937; const double c4 = 0.356563782; const double c5 = 0.319381530; const double c6 = 0.398942280401; const double negative = (x < 0 ? 1.0 : 0.0); const double xPos = (x < 0.0 ? -x : x); const double k = 1.0 / ( 1.0 + (c0 * xPos)); const double y1 =(((((((c1*k-c2)*k)+c3)*k)-c4)*k)+c5)*k; const double y2 = 1.0 - (c6*std::exp(-0.5*xPos*xPos)*y1); return ((1.0-negative)*y2) + (negative*(1.0-y2)); }
- include <cmath>
How to get a random value from a normal distribution ?
Use the Boost C++ library:int main() { const double mean = 10.0; const double sigma = 1.0; boost::normal_distribution<double> norm_dist(mean, sigma); boost::lagged_fibonacci19937 engine; //Make a histogram const int size = static_cast<int>(mean) * 2; std::vector<int> histo(size,0); for (int i=0; i!=1000000; ++i) { const double value = norm_dist.operator () <boost::lagged_fibonacci19937>((engine)); int index = value; index = (index < 0 ? 0 : index); index = (index > size - 1 ? size - 1 : index); ++histo[index]; } //Output histogram for (int i=0; i!=size; ++i) { std::cout << histo[i] << std::endl; } }
- include <boost/random/normal_distribution.hpp>
- include <boost/random/lagged_fibonacci.hpp>
- include <iostream>
- include <vector>
or in a function:
double normal(const double mean = 0.0, const double sigma = 1.0) { boost::normal_distribution<double> norm_dist(mean, sigma); static boost::lagged_fibonacci19937 engine; const double value = norm_dist.operator () <boost::lagged_fibonacci19937>((engine)); return value; }
- include <boost/random/normal_distribution.hpp>
- include <boost/random/lagged_fibonacci.hpp>
Code links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
