Skip to contents

Given a callable / fixture name and an integer seed, derive a stable R-side seed value via SHA-256, install it with [set.seed()], and return it invisibly. The matched Python helper morie._det_rng.from_seed(name, seed) builds a numpy.random.Generator from the same SHA digest so bootstrap / MCMC draws on the two sides agree to Monte-Carlo tolerance (and bit-identical when a deterministic-pseudo-bootstrap mode is plumbed).

Usage

morie_det_rng(name, seed)

Arguments

name

Character scalar; stable callable / fixture name. Must be identical to the string the Python side passes.

seed

Integer; user-supplied seed.

Value

Integer seed installed via set.seed() (invisibly).

Details

Mechanism: SHA-256(paste0(name, ":", seed)) is truncated to 32 bytes; bytes [9:12] (1-indexed, i.e. hex chars 17..24) form a 32-bit value reduced modulo 2^31 - 1 and passed to [set.seed()]. Bytes [1:8] are reserved for the Python Philox key; morie's morie/_det_rng.py is the Python counterpart.

Requires either the digest or openssl package for SHA-256. Both are widely available on CRAN; we try digest first, then openssl, and finally fall back to an internal pure-R SHA-256 implementation loaded only when neither is available. In practice CRAN reverse dependencies of morie ship with at least one of the two.

Examples

set.seed(1)
morie_det_rng("ksr07_bootstrap", 42L)
rnorm(5) # reproducible draws keyed by ("ksr07_bootstrap", 42)
#> [1]  1.6615528  1.4450543  0.4104904 -1.7492404 -0.6350112