-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathREADME.Rmd
121 lines (95 loc) · 3.65 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
output:
github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
[![R build status](https://github.com/daqana/dqrng/workflows/R-CMD-check/badge.svg)](https://github.com/daqana/dqrng/actions)
[![CRAN status](https://www.r-pkg.org/badges/version/dqrng)](https://cran.r-project.org/package=dqrng)
[![dqrng status badge](https://rstub.r-universe.dev/badges/dqrng)](https://rstub.r-universe.dev/dqrng)
[![Coverage status](https://codecov.io/gh/daqana/dqrng/branch/main/graph/badge.svg)](https://app.codecov.io/github/daqana/dqrng?branch=main)
[![Downloads](https://cranlogs.r-pkg.org/badges/dqrng?color=brightgreen)](https://www.r-pkg.org/pkg/dqrng)
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/2157/badge)](https://bestpractices.coreinfrastructure.org/projects/2157)
[![Dependencies](https://tinyverse.netlify.app/badge/dqrng)](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<dist>` 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).