Random-numbers are widely used for simulation, estimation and exploration. Many excellent pseudo-random number generators are available, and the literature keeps evolving. George Marsaglia had introduced a number of these, as well as the test battery diehard (later extended by Robert Brown in dieharder, see also the github repo).
A particular generator, Ziggurat, was introduced in a JSS paper in 2000 by Marsaglia and Tsang. A small correction appeared in a JSS paper in 2005 by Leong et al. This version, along with improvements due to Burkhardt and Voss, is provided in our R package RcppZiggurat, along with other implementations allowing for detailed comparison.
However, this existing R package provides a heavier build, and imposes run-time limits as it also links to the GNU GSL (for the method by Voss) as well as implementation from gretl and quantlib. The new package assembled here offers a lighter-weight alternative. It can be called directly from R, and also offers a C(++)-callable interface other packages can use in their native code as we demonstrate via four different sample client packages each connecting in a slightly different way.
The following chart is generated by the example script demo/timings.R
It shows that the speed gains from zigg
relative to base
R are (for this example, on our machine) on the order of 7.4, 5.2, and
4.7 for the normal, exponential, and uniform RRNGs.
That said, the actual time drawing random numbers will likely only affect a fraction of the run-time of study so one may well consider sticking with either the high-quality, well-tested, and widely-deployed default generators available in R, or in other CRAN package. This package provides a useful illustration of how and older and simpler generator can be lighter and faster (at possibly a lesser total period etc).
The preceding demo/timings.R shows the basic usage from R. This works as usual:
library(zigg) # load package
zrnorm(5) # draw five N(0,1) distributed variate
The package can also be used in compiled codeāeven in four different ways. All are demonstrated in included demo packages and perform similarly but offer different approaches.
src/init.c
using a standard R mechanism for as exported
object code that can be called directly (and consult the Writing
R Extensions manual for details);The code is provided as an R package so a standard installation from the repository via
::install_packages("zigg") remotes
work. The package has no dependencies.
Moreover, the package can be installed (as binary, where available, or source) from its r-universe repository via
<- c("https://eddelbuettel.r-universe.dev", "https://cloud.r-project.org")
urls install.packages('zigg', repos = urls)
and is also available as an Ubuntu binary, see the docs for that).
Dirk Eddelbuettel
GPL (>= 2)