I wrote the following code in Rcpp, for generating Gamma Distribution random variables, however each time that I run it I take the same output. I read that in order to have different realizations I have to use a random generator as explained here Why is this random generator always output the same number
The code that I use is the following, am I doing something wrong?? Basically, I feel that I use the exact same code as in the question posted, but for some reason in my case doesn't work.
#include <RcppArmadilloExtensions/sample.h>
#include <random>
#include <iostream>
#include <math.h>
#include<Rmath.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
double Gama_Draw_1(double a , double b){
std::default_random_engine generator;
std::gamma_distribution<double> d(a,b);
return d(generator);
}
// [[Rcpp::export]]
double Gama_Draw_2(double a , double b){
std::mt19937 prng{ std::random_device{}() };
std::gamma_distribution<double> d(a,b);
return d(prng);
}
// [[Rcpp::export]]
arma::vec Gama_Draw_3(double a , double b){
arma::vec S(10);
std::random_device rd;
std::mt19937 gen(rd());
std::gamma_distribution<> distrib(a, b);
for(int i=0; i<10; ++i){
S[i] = distrib(gen);
}
return S;
}
For example,
Gama_Draw_1(1,1)
[1] 0.5719583
Gama_Draw_2(1,1)
[1] 1.065146
Gama_Draw_3(1,1)
[,1]
[1,] 1.0651459
[2,] 0.8683230
[3,] 2.7298295
[4,] 0.7930546
[5,] 0.3722135
[6,] 0.7317269
[7,] 0.2569218
[8,] 2.0916565
[9,] 0.9033717
[10,] 1.4198487
No matter how many times I run it I always get the same result.