mercredi 20 janvier 2021

how to generate random value probability distribution in c++

I try generate value from above prob distribution i need must provide generate number without IF ELSE

if(u < c * p[Y-1] / q[Y-1]){ X = Y; cout << "Random Number = " << X; } this section decide to number is random or not but I need must provide generate number without if else i want to use loop logic How can i?

#include<iostream>
#include <stdlib.h>
#include <cmath>
#include<ctime>

using namespace std;

int main(){
    int X,Y;
    float u,c;

     float p[10] = {0.11,0.12,0.09,0.08,0.12,0.10,0.09,0.09,0.10,0.10};
     float q[10] = {0.10,0.10,0.10,0.10,0.10,0.10,0.10,0.10,0.10,0.10};

    c = 1.2;

    srand(time(NULL));

    u = (float)rand()/RAND_MAX;

    Y = floor(u * 10) + 1;
    u = (float)rand()/RAND_MAX;

    if(u < c * p[Y-1] / q[Y-1]){
        X = Y;
        cout << "Random Number = " << X;
    }else{
        cout << "Random Number could not be generated";
    }
    return 0;
}



Aucun commentaire:

Enregistrer un commentaire