user1066351
user1066351

Reputation: 1

generate random number using chi square distribution c++

I need to generate random numbers representing user activities using chi square distribution in (boost) tr1 c++. Any example or help on how would I start?


I tried the following code:

int main (){
    std::tr1::mt19937 eng; // a core engine class 
                           //mt19937 is a very fast random number generator algorithm 
    eng.seed(time(0));   //each engine has a seed method 

    //file to store seed 
    std::tr1::chi_squared_distribution<double> chdist(5.0); 
    cout<<endl<<"CHIDIST"<<endl<<"================"<<endl; 

    for (int i = 0; i<50; ++i) 
    { 
         Act.push_back(chdist(eng)*0.1); 
         int rounded = ((int)(chdist(eng) * 100 + .5) / 100.0);         
         Act.push_back(rounded*0.1); cout<<Act[i]<< endl; 
    }

     return 0;   
}

Upvotes: 0

Views: 2143

Answers (1)

varepsilon
varepsilon

Reputation: 488

From Wikipedia:

If Z1, ..., Zk are independent, standard normal random variables, then the sum of their squares is distributed according to the chi-squared distribution

so you may want to generate k standard normal variables and calculate their sum squared.

Upvotes: 2

Related Questions