understanding-prngs.Rmd
Pseudo-random number generators (PRNGs) power much of what goes on
“behind the scenes” in statistical and cryptographic settings. In
R
, there is a pseudo-random number generator present which
allows users to generate random variables from a variety of
distributions. To check the default PRNG used by your R
version you can run the following:
# Using version 4.2.2 (at time of writing)
# Returns the methods used for
# 1. The "default" random number generation,
# 2. Normal variable generation
# 3. Discrete uniform variable generation
RNGkind()
#> [1] "Mersenne-Twister" "Inversion" "Rejection"
The PRNG which R utilizes can be specified by specifying a
.Random.seed
argument in the beginning of an R script or by
adjusting the default kind
(and normal.kind
and sample.kind
) arguments in the RNGkind
or
set.seed
argument.
While it is possible for one to utilize their own PRNG through use of
user-supplied random number generation, such an approach is
pedagogically complex. For users interested in working with and learning
about random number generation, present tools available still leave a
degree of complexity and/or mystery around how random numbers are
generated from the seed(s) and parameters supplied to a given PRNG. The
randngen
package provides a suite of PRNGs to which aim to
be easy to use and flexible for understanding the relevant maths and
algorithms implemented by allowing users to specify all relevant
parameters.
library(randngen)
# Function from here: https://stackoverflow.com/questions/5468280/scale-a-series-between-two-points
range01 <- function(x){(x-min(x))/(max(x)-min(x))}
randngen::lcg(seed = 1234, n=10000)|>
range01()|>
hist(main= "0-1 Scaled Uniform Random Variables with randgen::lcg()")
Disclaimer: This section was copied from output from ChatGPT. I have not found information on this collected in one individual place. However I was told by ChatGPT that it “[…] is well-documented in R’s official NEWS files, which detail changes and new features introduced in each R version …”. If this information is misleading or false, please open an issue or submit a pull request with more accurate information.
R’s base distributions have evolved in their use of Pseudo-Random Number Generators (PRNGs) over time to improve accuracy, speed, and security. Each R version may implement different PRNGs depending on updates, which can impact reproducibility for code across versions. Here’s a summary of the key PRNGs used across different base R versions and the changes introduced over time.
sample()
.
This update improved randomness in sampling without replacement by
ensuring each permutation has equal probability, which impacted
reproducibility of code relying on sampling in previous versions.RNGkind()
function
allows users to set RNG kinds explicitly for reproducibility, with
settings such as "Mersenne-Twister"
,
"L'Ecuyer-CMRG"
, and newer options like
"Xoroshiro128+"
for applications needing faster,
non-cryptographic random numbers.R Version | Default PRNG | Other Available PRNGs | Notes |
---|---|---|---|
R < 1.0.0 | Basic LCG | - | Simple LCGs with limited randomness quality. |
R 1.0.0 - 2.0.0 | Mersenne Twister | L’Ecuyer-CMRG | First use of Mersenne Twister as the default PRNG. |
R 2.1.0 - 2.14.0 | Mersenne Twister | Super-Duper, Knuth-TAOCP-2002 | Additional PRNGs introduced for specific needs. |
R 2.15.0 - 3.5.0 | Mersenne Twister | Marsaglia-Multicarry, updated L’Ecuyer-CMRG | Added Marsaglia-Multicarry for faster random numbers. |
R 3.6.0 | Mersenne Twister | - | Updated sampling algorithm for better random permutations. |
R 4.0.0 - 4.1.0 | Mersenne Twister | L’Ecuyer-CMRG, 64-bit support | 64-bit integer support for PRNGs. |
R 4.2.0+ | Mersenne Twister | Xoroshiro128+ | Introduced Xoroshiro128+ for faster generation. |
R allows explicit control of the RNG type and seed via
RNGkind()
and set.seed()
, respectively,
ensuring reproducibility even as PRNG options expand.
Random function - RDocumentation. https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Random.
Morris, T. P., White, I. R. & Crowther, M. J. Using simulation studies to evaluate statistical methods. Statistics in Medicine 38, 2074–2102 (2019).
Generating distributions from random number generators. Cross Validated https://stats.stackexchange.com/questions/637706/generating-distributions-from-random-number-generators.
L’Ecuyer, P. & Simard, R. TestU01. ACM Transactions on Mathematical Software 33, 1–40 (2007).