title: Generate Random Gamma in Rcpp
body: <p>I have vector shape and scale parameters to generate numbers from random gamma distribution. I couldn't find a way in R like;</p>
  
  <pre><code>lambda&lt;-matrix(rgamma(p,ak,scale=1/bk),p,1)
  </code></pre>
  
  <p>So I'm trying to generate in a loop and it seems I lose time which is the main purpose to use Rcpp.</p>
  
  <pre><code>for(int i = 0; i&lt;p;i++){
  lambda(i)=arma::conv_to&lt;double&gt;::from(arma::randg&lt;arma::mat (1,1,arma::distr_param(ak(i),pow(bk(i),-1))));
  }
  </code></pre>
  
  <p>edit: I have simulated all 3 methods and compare time.</p>
  
  <pre><code>// [[Rcpp::plugins(cpp11)]]
  // [[Rcpp::depends(RcppArmadillo)]]
  #include &lt;RcppArmadillo.h&gt;   
  // [[Rcpp::export]]
  
  arma::mat rg(const arma::mat&amp; w,double scale,int num) {
  int p = w.n_rows;
  arma::mat lambda(num,p,arma::fill::zeros);  
  
  
  for (int j=0;j&lt;num;j++){
  for(int i = 0; i&lt;p;i++){
      lambda(j,i)=arma::conv_to&lt;double&gt;::from(arma::randg&lt;arma::mat&gt;(1,1,arma::distr_param(w(i),scale)));
  }
  }
  return mean(lambda.rows(0,num-1),0);    
  }
  </code></pre>
  
  <p>These are R codes; </p>
  
  <pre><code>fun&lt;-function(w,sca,num){
  p=nrow(w)
  lambda=matrix(0,num,p)
  for (jj in 1:num){
      for (i in 1:p){
          lambda[jj,i]=rgamma(1,w[i],scale=sca)
      }
  }
  return(colMeans(lambda))
  }
  
  fun2&lt;-function(w,sca,num){
  p=nrow(w)
  lambda=matrix(0,num,p)
  for (jj in 1:num){      
          lambda[jj,]=rgamma(p,w,scale=sca)       
  }
  return(colMeans(lambda))
  }
  </code></pre>
  
  <p>Here is result;</p>
  
  <pre><code>  a=matrix(c(1,2,3))
  
  
    microbenchmark(rg(a,2,100000),fun(a,2,1e5),fun2(a,2,1e5))
    Unit: milliseconds
                expr       min        lq      mean    median        uq       max neval
      rg(a, 2, 1e+05)  891.6197  900.0092  952.3833  915.6609  991.7918 1271.9117   100
      fun(a, 2, 1e+05) 1018.6645 1059.3994 1171.9160 1130.7481 1200.3192 1754.3445   100
     fun2(a, 2, 1e+05)  321.8309  339.2670  373.5317  355.5914  395.1515  604.4365   100
  </code></pre>