# Normal Distribution in C++

In this tutorial, we will learn about **normal distribution and its implementation in C++. **Before proceeding further let’s first understand what is a normal distribution.

**Normal Distribution: **

- It is a continuous probability distribution.
- A continuous random variable X is said to follow the normal distribution with parameters μ (called mean) and σ² (called variance) if its probability density function is given by

**P(x)= (1/√2πσ²)* e ^{-( x-μ )²/2σ²}**

where **μ** is the mean of the distribution

**σ** is the standard deviation and

**σ²** is the variance.

**Note for implementation:**

- Make sure
**C++11**support is enabled in the compiler of your IDE. Otherwise, it will give you errors. I am using Dev C++ so I will tell you the steps of enabling c++11 support in Dev C++. These steps will be similar for other IDE. So the steps are as follows: 1) Go to**file -> create new project**2) Select**Empty Project**3) Type project name and click ‘ok’ 4) Type filename and click ‘save’ 5) Go to**Project -> Project Options**6) Go to parameters tab and type “**-std=c++11**” under C++ compiler section. 7) Type and run the below code. - It is not necessary that you will get the same output that I have got. Each time when you run below code you will get different output (distribution).

## Program to implement Normal Distribution in C++

#include<iostream> #include<chrono> #include<random> using namespace std; int main() { /* Create random engine with the help of seed */ unsigned seed = chrono::steady_clock::now().time_since_epoch().count(); default_random_engine e (seed); /* declaring normal distribution object 'distN' and initializing its mean and standard deviation fields. */ /* Mean and standard deviation are distribution parameters of Normal distribution. Here, we have used mean=5, and standard deviation=2. You can take mean and standard deviation as per your choice */ normal_distribution<double> distN(5,2); int n; cout<<"\nEnter no. of samples: "; cin>>n; cout<<"\nNormal distribution for "<<n<<" samples (mean=5, standard deviation=2) =>\n"; for (int i=1; i<=n; i++) { cout<<i<<". "<<distN(e)<<"\n"; } return 0; }

**Input/Output:**

Enter no. of samples: 10 Normal distribution for 10 samples (mean=5, standard deviation=2) => 1. 6.01492 2. 7.18826 3. 4.44432 4. 6.03708 5. 5.93383 6. 4.43706 7. 8.45193 8. 3.75018 9. 1.58229 10. 4.0596

**You may also learn:**

## Leave a Reply

You must be logged in to post a comment.