MyNixOS website logo
Description

Lightweight Interfaces to the 'Ziggurat' Pseudo Random Number Generator.

The 'Ziggurat' pseudo-random number generator (or PRNG), introduced by Marsaglia and Tsang (2000, <doi:10.18637/jss.v005.i08>) and further improved by Leong et al (2005, <doi:10.18637/jss.v012.i07>), offers a lightweight and very fast PRNG for the normal, exponential, and uniform distributions. It is provided here in a small zero-dependency package. It can be used from R as well as from 'C/C++' code in other packages as is demonstrated by four included sample packages using four distinct methods to use the PRNG presented here in client package. The implementation is influenced by our package 'RcppZiggurat' which offers a comparison among multiple alternative implementations but presented here in a lighter-weight implementation that is easier to use by other packages. The PRNGs provided are generally faster than the ones in base R: on our machine, the relative gains for normal, exponential and uniform are on the order of 7.4, 5.2 and 4.7 times faster than base R. However, these generators are of potentially lesser quality and shorter period so if in doubt use of the base R functions remains the general recommendation.

zigg: lightweight interface to Ziggurat

CI License CRAN r-universe Dependencies Downloads Last Commit

Motivation

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.

Example

The following chart is generated by the example script example/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).

Usage

Direct R Use

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

Source Use In Packages

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.

  • zigguserDirectC declares the (remote) functions and instantiates function pointers in 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);
  • zigguserDirectCpp is similar but assigns the functions pointers inside an included header file in a somewhat more idiomatic C++ way;
  • zigguserHeaderCpp may be the easier way for C++ programmers familiar with header-only libraries as this package provides access to Ziggurat via one such header; the remainder is then standard use of compiled functions in an R package.
  • zigguserHeaderRcpp gets to use additional Rcpp machinery to make package building simpler, it also adds a layer parameter testing adding to a small performance penalty relative to the other three (more bare-bones) approaches.

Installation

The code is provided as an R package so a standard installation from the repository via

remotes::install_packages("zigg")

work. The package has no dependencies.

Moreover, the package can be installed (as binary, where available, or source) from its r-universe repository via

urls <- c("https://eddelbuettel.r-universe.dev", "https://cloud.r-project.org")
install.packages('zigg', repos = urls)

and is also available as an Ubuntu binary, see the docs for that).

Author

Dirk Eddelbuettel

License

GPL (>= 2)

Metadata

Version

0.0.2

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows