Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
Bias-free alternative to R's 'sample' function
R C++
Branch: master
Clone or download
Cannot retrieve the latest commit at this time.
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R
inst/include
man
src
tests
vignettes
.Rbuildignore
.gitignore
.travis.yml
DESCRIPTION
NAMESPACE
README.Rmd
README.md
appveyor.yml
dqsample.Rproj
tic.R

README.md

Travis build status AppVeyor build status CRAN status Coverage status

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>) is used. The package is motivated by a thread on R-devel.

Installation

At the moment dqsample is not on CRAN, but you can install the current version via drat:

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:

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:

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.

You can’t perform that action at this time.