Miranda
Fast PRNGs for R
Install / Use
/learn @coolbutuseless/MirandaREADME
miranda <img src="man/figures/miranda-logo.jpg" align="right" height=230/>
<!-- badges: start --> <!-- badges: end -->The miranda packages includes some modern pseudo-random number
generators (PRNGs). These are up to 20x faster than the built-in PRNGs
in R.
PRNG summary
| PRNG | BigCrush result | Reference | |-------------|-------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------| | xoshiro256+ | “No Systematic Failures” (*) | http://prng.di.unimi.it/ | | lehmer64 | Passes | Lemire’s blog | | splitmix64 | Passes | source | | romutrio | Passes | romu-random.org |
Installation
You can install from GitHub with:
# install.package('remotes')
remotes::install_github('coolbutuseless/miranda')
What’s in the box:
Common Functions
| Function | Purpose |
|---------------|-----------------------------------------------------------------------------|
| set_seed_* | Set a seed to initialise the state of the PRNG |
| get_state_* | Fetch the internal state of the PRNG as raw bytes. Length varies with PRNG |
| set_state_* | Write raw bytes into the internal state of the PRNG Length varies with PRNG |
| runif1_* | Draw a single random value from the PRNG |
| runif_* | Draw multiple values from the PRNG |
Special functions
| Function | Purpose |
|---------------------------------------------|--------------------------------------------------------------------|
| jump_xoshiro256p, long_jump_xoshiro256p | Advance the state of the xoshiro256+ PRNG |
| runif_splitmix64_as_bytes | Same as runif_splitmix64 except values are returned as raw bytes |
xoshiro256p
library(miranda)
# Set the seed
set_seed_xoshiro256p(2020)
# Get the internal state of the generator. 32 bytes.
(state <- get_state_xoshiro256p())
#> [1] ee 6d 45 f5 0b 1c e9 8e 81 89 5b 6b ee ca 13 07 d9 3f 76 fe 13 4f c2 33 44
#> [26] 3c a9 de 5e b7 56 f9
# Generate some uniform random numbers from xoshiro256+
runif_xoshiro256p(5)
#> [1] 0.5322239 0.7788869 0.4601653 0.8475389 0.9540731
# Jump ahead. As if 2^128 numbers had been generated
jump_xoshiro256p()
# Generate some uniform random numbers from xoshiro256+
runif_xoshiro256p(5)
#> [1] 0.5714143 0.5006207 0.6907487 0.2625934 0.5053289
# reset the state to a prior value
set_state_xoshiro256p(state)
# Generate some uniform random numbers from xoshiro256+
runif_xoshiro256p(5)
#> [1] 0.5322239 0.7788869 0.4601653 0.8475389 0.9540731
Benchmark: Generating 1 million uniform random numbers
<details> <summary> Click to show/hide bench::mark() code </summary>N <- 1e6
res <- bench::mark(
runif_xoshiro256p(N),
runif_lehmer64(N),
runif_splitmix64(N),
runif_romutrio(N),
runif(N),
check = FALSE
)
</details>
<img src="man/figures/README-bench-1.png" width="100%" />
- The PRNGs in this package all roughly run at the same speed
- At their best, these PRNGs are about 20x faster than
runif()
Related Software
- dqrng - Fast PRNGs
(xoshiro, PCG) with an interface compatible with
runif(). Also suppliesrnorm() - random a package for true random numbers fetched from random.org
Acknowledgements
- R Core for developing and maintaining such a wonderful language.
- CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining the repository
Related Skills
node-connect
342.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
342.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.7kCommit, push, and open a PR
