I am developing some sampling algorithms based on RcppZiggurat. My objective is to use the samplers in cpp code but not having to come up with new ways of setting the seed but to rely on existing functions, such as RcppZiggurat::zsetseed.
This are my two cpp functions using the sampler zigg.norm():
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <Ziggurat.h>
// [[Rcpp::depends(RcppZiggurat)]]
using namespace Rcpp;
using namespace arma;
static Ziggurat::Ziggurat::Ziggurat zigg;
// [[Rcpp::export]]
vec zzrnorm(int n) {
vec x(n);
for (int i=0; i<n; i++) {
x(i) = zigg.norm();
}
return x;
}
// [[Rcpp::export]]
vec zzrnorm_ss(int n, unsigned long int set_seed) {
zigg.setSeed(set_seed);
vec x(n);
for (int i=0; i<n; i++) {
x(i) = zigg.norm();
}
return x;
}
And here is the R code and outputs that I am studying and cannot understand the outputs:
> Rcpp::sourceCpp("so_Ziggurat.cpp")
> RcppZiggurat::zsetseed(1)
> RcppZiggurat::zrnorm(3)
[1] -1.4808858 0.9991196 -0.4898021
> RcppZiggurat::zsetseed(1)
> RcppZiggurat::zrnorm(3)
[1] -1.4808858 0.9991196 -0.4898021
> RcppZiggurat::zsetseed(1)
> zzrnorm(3)
[,1]
[1,] -1.1409054
[2,] 0.8759027
[3,] -0.1969075
> RcppZiggurat::zsetseed(1)
> zzrnorm(3)
[,1]
[1,] -0.1245053
[2,] 0.2695581
[3,] -0.4185105
> zzrnorm_ss(3, 1)
[,1]
[1,] -1.4808858
[2,] 0.9991196
[3,] -0.4898021
> zzrnorm_ss(3, 1)
[,1]
[1,] -1.4808858
[2,] 0.9991196
[3,] -0.4898021
The first two generators are from the RcppZiggyrat and use RcppZiggurat::zsetseed to set seed and RcppZiggurat::zrnorm to generate random numbers. They are reproducible, of course.
The second set of outputs is something I don't understand. I use the package provided RcppZiggurat::zsetseed to set seed and my function zzrnorm to sample random numbers. My function uses the same class of samplers for which RcppZiggurat::zsetseed sets the seed based on
static Ziggurat::Ziggurat::Ziggurat zigg;
But RcppZiggurat::zsetseed does not set the seed, and the generated numbers differ despite the fact that RcppZiggurat::zsetseed uses the same source code: zigg.setSeed(set_seed); to set the seed.
Now, if I include RcppZiggurat::zsetseed within my function as in the third example then the seed is set properly and the results are reproducible.
So, why isn't that the case in the second set of outputs?
I would greatly appreciate some suggestions as this would be my preferred way of working with the samplers in R.