[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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.

  1. include <cmath>
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)); }


How to get a random value from a normal distribution ?

Use the Boost C++ library:

  1. include <boost/random/normal_distribution.hpp>
  2. include <boost/random/lagged_fibonacci.hpp>
  3. include <iostream>
  4. include <vector>
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; } }


or in a function:
  1. include <boost/random/normal_distribution.hpp>
  2. include <boost/random/lagged_fibonacci.hpp>
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; }


Code links



last edited (May 19, 2007) by bilderbikkel, Number of views: 1726, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.