# C++ Program to Generate Random Numbers using Probability Distribution Function

«
»
This is a C++ Program to generate random numbers using Probability Distribution Function. Probability distribution is based on probability density function. a probability density function (pdf), or density of a continuous random variable, is a function that describes the relative likelihood for this random variable to take on a given value. The probability of the random variable falling within a particular range of values is given by the integral of this variable’s density over that range—that is, it is given by the area under the density function but above the horizontal axis and between the lowest and greatest values of the range.

Here is source code of the C++ Program to Generate Random Numbers Using Probability Distribution Function. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `//pdf(x) = 1 if x>360`
2. `//       = 0 if x<0`
3. `//       = x/360 otherwise`
4. `#include <iostream>`
5. `#include <math.h>`
6. `#include <stdlib.h>`
7. ` `
8. `using namespace std;`
9. ` `
10. `//This is a sample program to generate a random numbers based on probability desity function of spiner`
11. `//pdf(x) = 1 if x>360`
12. `//       = 0 if x<0`
13. `//       = x/360 otherwise`
14. `int N = 10;`
15. `int main(int argc, char **argv)`
16. `{`
17. `    int p = 0;`
18. `    for (int i = 0; i < N; i++)`
19. `    {`
20. `        p = rand() % 400;`
21. `        if (p > 360)`
22. `            cout << 0 << " ";`
23. `        else if (p < 0)`
24. `            cout << 0 << " ";`
25. `        else`
26. `            cout << p * 0.1 / 360 << " ";`
27. `    }`
28. `    cout << "...";`
29. `}`

Output:

```\$ g++ ProbabilityDist.cpp
\$ a.out

0.0113889 0.0186111 0.0927778 0.0277778 0 0.0344444 0.0772222 0.0438889 0.045 0.0177778 ...```

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now! 