SkillAgentSearch skills...

Miranda

Fast PRNGs for R

Install / Use

/learn @coolbutuseless/Miranda
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<!-- README.md is generated from README.Rmd. Please edit that file -->

miranda <img src="man/figures/miranda-logo.jpg" align="right" height=230/>

<!-- badges: start -->

R build
status Lifecycle:
experimental

<!-- 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 supplies rnorm()
  • 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

View on GitHub
GitHub Stars22
CategoryDevelopment
Updated2mo ago
Forks1

Languages

C

Security Score

85/100

Audited on Jan 7, 2026

No findings