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

C++Server Side ProgrammingProgramming

Probability Density Function (pdf), is a function that describes the relative likelihood for this random variable to take on a given value. It is also called as density of a continuous random variable.

The probability of the random variable fall within a particular range of values is given by the integral of this variable’s density over that range, So, 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. Probability Distribution is based upon this probability density function.

## Algorithm

Begin
Declare n
Assign pdf=0
For i =0 to n , do
pdf = rand() mod 200
If pdf greater than 360
Print 1
Else if pdf less than 0
Print 0
Else
Print pdf * 0.1 / 360
Done
Done
end

## Example Code

#include <iostream>
using namespace std;
int n = 6;
int main(int argc, char **argv) {
int pdf = 0;
for (int i = 0; i < n; i++) {
pdf = rand() % 200;
if (pdf > 360)
cout << 1 << " ";
else if (pdf < 0)
cout << 0 << " ";
else
cout << pdf * 0.1 / 360 << " ";
}
cout << "...";
}

## Output

0.0508333 0.0238889 0.0491667 0.0319444 0.0536111 0.0375 ...
Published on 15-Mar-2019 06:11:52