Saturday, 18 November 2017

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


Code:

#include   iostream
#include   math.h
#include   stdlib.h

using namespace std;

//This is a sample program to generate a random numbers based on probability desity function of spiner
//pdf(x) = 1 if x>360
//       = 0 if x<0 font="">
//       = x/360 otherwise
int N = 10;
int main(int argc, char **argv)
{
    int p = 0;
    for (int i = 0; i < N; i++)
    {
        p = rand() % 400;
        if (p > 360)
            cout << 0 << " ";
        else if (p < 0)
            cout << 0 << " ";
        else
            cout << p * 0.1 / 360 << " ";
    }
    cout << "...";
}


Output:

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



More C++ Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...