Movatterモバイル変換


[0]ホーム

URL:


Dirk Eddelbuettel

zigg: Lightweightinterface to Ziggurat

CILicenseCRANr-universeDependenciesDownloadsLast Commit

Motivation

Random-numbers are widely used for simulation, estimation andexploration. Many excellent pseudo-random number generators areavailable, and the literature keeps evolving.GeorgeMarsaglia had introduced a number of these, as well as the testbatterydiehard (laterextended byRobert Brownindieharder,see also thegithubrepo).

A particular generator,Ziggurat,was introduced in aJSSpaper in 2000 byMarsaglia andTsang. A small correction appeared in aJSS paper in 2005 byLeong et al. This version, along with improvements due toBurkhardt andVoss, is provided inour R packageRcppZiggurat,along with other implementations allowing for detailed comparison.

However, this existing R package provides a heavier build, andimposes run-time limits as it also links to the GNU GSL (for the methodbyVoss) as well asimplementation fromgretlandquantlib. The new packageassembled here offers a lighter-weight alternative. It can be calleddirectly from R, and also offers a C(++)-callable interface otherpackages can use in their native code as we demonstrate via fourdifferent sample client packages each connecting in a slightly differentway.

Example

The following chart is generated by the example scriptexample/timings.R

It shows that the speed gains fromzigg relative to baseR are (for this example, on our machine) on the order of 7.4, 5.2, and4.7 for the normal, exponential, and uniform RRNGs.

That said, the actual time drawing random numbers will likely onlyaffect a fraction of the run-time of study so one may well considersticking with either the high-quality, well-tested, and widely-deployeddefault generators available in R, or in other CRAN package. Thispackage provides a useful illustration of how and older and simplergenerator can be lighter and faster (at possibly a lesser total periodetc).

Usage

Direct R Use

The precedingdemo/timings.Rshows the basic usage from R. This works as usual:

library(zigg)# load packagezrnorm(5)# draw five N(0,1) distributed variate

Source Use In Packages

The package can also be used in compiled code—even infourdifferent ways. All are demonstrated in included demo packages andperform similarly but offer different approaches.

  • zigguserDirectCdeclares the (remote) functions and instantiates function pointers insrc/init.c using a standard R mechanism for as exportedobject code that can be called directly (and consult theWritingR Extensions manual for details);
  • zigguserDirectCppis similar but assigns the functions pointers inside an included headerfile in a somewhat more idiomatic C++ way;
  • zigguserHeaderCppmay be the easier way for C++ programmers familiar with header-onlylibraries as this package provides access toZiggurat viaone such header; the remainder is then standard use of compiledfunctions in an R package.
  • zigguserHeaderRcppgets to use additionalRcpp machineryto make package building simpler, it also adds a layer parameter testingadding to a small performance penalty relative to the other three (morebare-bones) approaches.

Installation

The code is provided as an R package so a standard installation fromthe 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 itsr-universerepository 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, seethedocs for that).

Author

Dirk Eddelbuettel

License

GPL (>= 2)

Initially created: Fri Jan 31 08:20:49 AM CST 2025
Last modified: Fri Jan 31 13:43:50 CST 2025


[8]
ページ先頭

©2009-2025 Movatter.jp