-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
61 lines (48 loc) · 2.14 KB
/
Copy pathREADME.Rmd
File metadata and controls
61 lines (48 loc) · 2.14 KB
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
[](https://travis-ci.org/daqana/dqsample)
[](https://ci.appveyor.com/project/rstub/dqsample)
[](https://cran.r-project.org/package=dqsample)
[](https://codecov.io/github/daqana/dqsample?branch=master)
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
set.seed(42)
```
# dqsample
The `base::sample` function uses a slightly biased algorithm for
creating random integers within a given range. As an alternative the
algorithm suggested by Daniel Lemire (2018,
<[arXiv:1805.1094](https://arxiv.org/abs/1805.10941)>) is used.
The package is motivated by a [thread on R-devel](https://stat.ethz.ch/pipermail/r-devel/2018-September/076817.html).
## Installation
At the moment dqsample is not on CRAN, but you can install the current version
via [drat](https://cran.r-project.org/package=drat):
```r
if (!requireNamespace("drat", quietly = TRUE)) install.packages("drat")
drat::addRepo("daqana")
install.packages("dqsample")
```
## Example
When sampling many random integers the density of odd and even numbers should be roughly equal and constant.
This is not the case with `base::sample`:
```{r base}
m <- 2/5 * 2^32
x <- base::sample(floor(m), 1000000, replace = TRUE)
plot(density(x[x %% 2 == 0]), main = "base::sample")
lines(density(x[x %% 2 == 1]), col = 2)
```
While it is the case with `dqsample::sample`:
```{r dqsample}
m <- 2/5 * 2^32
x <- dqsample::sample(floor(m), 1000000, replace = TRUE)
plot(density(x[x %% 2 == 0]), main = "dqsample::sample")
lines(density(x[x %% 2 == 1]), col = 2)
```
This particular sample for the bias was found by [Duncan Murdoch](https://stat.ethz.ch/pipermail/r-devel/2018-September/076827.html).