https://github.com/daqana/dqrng
Fast Pseudo Random Number Generators for R
https://github.com/daqana/dqrng
cran r r-package random random-distributions random-generation random-sampling rng
Last synced: 3 months ago
JSON representation
Fast Pseudo Random Number Generators for R
- Host: GitHub
- URL: https://github.com/daqana/dqrng
- Owner: daqana
- License: other
- Created: 2018-04-18T19:23:54.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-04-13T22:35:43.000Z (over 1 year ago)
- Last Synced: 2024-04-14T10:44:37.744Z (over 1 year ago)
- Topics: cran, r, r-package, random, random-distributions, random-generation, random-sampling, rng
- Language: C++
- Homepage: https://daqana.github.io/dqrng/
- Size: 4.32 MB
- Stars: 33
- Watchers: 7
- Forks: 8
- Open Issues: 17
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.note
Awesome Lists containing this project
README
---
output:
github_document
---```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```[](https://github.com/daqana/dqrng/actions)
[](https://cran.r-project.org/package=dqrng)
[](https://rstub.r-universe.dev/dqrng)
[](https://app.codecov.io/github/daqana/dqrng?branch=main)
[](https://www.r-pkg.org/pkg/dqrng)
[](https://bestpractices.coreinfrastructure.org/projects/2157)
[](https://cran.r-project.org/package=dqrng)# dqrng
The dqrng package provides fast random number generators (RNG) with good statistical properties for usage with R.
It combines these RNGs with fast distribution functions to sample from uniform, normal or exponential distributions.
Both the RNGs and the distribution functions are distributed as C++ header-only library.## Installation
The currently released version is available from CRAN via
```r
install.packages("dqrng")
```Intermediate releases can also be obtained via
[r-universe](https://rstub.r-universe.dev/dqrng):```r
install.packages('dqrng', repos = c(
rstub = 'https://rstub.r-universe.dev',
CRAN = 'https://cloud.r-project.org'))
```## Example
Using the provided RNGs from R is deliberately similar to using R's build-in RNGs:
```{r example}
library(dqrng)
dqset.seed(42)
dqrunif(5, min = 2, max = 10)
dqrexp(5, rate = 4)
```They are quite a bit faster, though:
```{r performance}
N <- 1e4
bm <- bench::mark(rnorm(N), dqrnorm(N), check = FALSE)
bm[, 1:4]
```This is also true for the provided sampling functions with replacement:
```{r sampling1}
m <- 1e7
n <- 1e5
bm <- bench::mark(sample.int(m, n, replace = TRUE),
sample.int(1e3*m, n, replace = TRUE),
dqsample.int(m, n, replace = TRUE),
dqsample.int(1e3*m, n, replace = TRUE),
check = FALSE)
bm[, 1:4]
```And without replacement:
```{r sampling2}
bm <- bench::mark(sample.int(m, n),
sample.int(1e3*m, n),
sample.int(m, n, useHash = TRUE),
dqsample.int(m, n),
dqsample.int(1e3*m, n),
check = FALSE)
bm[, 1:4]
```Note that sampling from `10^10` elements triggers "long-vector support" in R.
In addition the RNGs provide support for multiple independent streams for parallel usage:
```{r parallel}
N <- 1e7
dqset.seed(42, 1)
u1 <- dqrunif(N)
dqset.seed(42, 2)
u2 <- dqrunif(N)
cor(u1, u2)
```It is also possible to register the supplied generators as user-supplied RNGs. This way `set.seed()` and `dqset.seed()` influence both `(dq)runif` and `(dq)rnorm` in the same way. This is also true for other `r` functions, but note that `rexp` and `dqrexp` still give different results:
```{r register}
register_methods()
set.seed(4711); runif(5)
set.seed(4711); dqrunif(5)
dqset.seed(4711); rnorm(5)
dqset.seed(4711); dqrnorm(5)
set.seed(4711); rt(5, 10)
dqset.seed(4711); rt(5, 10)
set.seed(4711); rexp(5, 10)
set.seed(4711); dqrexp(5, 10)
restore_methods()
```## Feedback
All feedback (bug reports, security issues, feature requests, ...) should be provided as [issues](https://github.com/daqana/dqrng/issues).