get_seed: Random Seed Recovery from RNG State

View source: R/get_seed.R

get_seedR Documentation

Random Seed Recovery from RNG State

Description

Functions for recovering the original seed value that produced the current random number generator state. Provides both R and C++ implementations with the C++ version offering significantly improved performance for large search spaces.

Usage

get_seed(range = NULL, fallback_seed = 42, max_search = 2147483647,
  step_size = 50000, use_cpp = TRUE, ...)

get_seed_cpp(range = NULL, fallback_seed = 42, max_search = 2147483647,
  step_size = 50000, batch_size = 10000, verbose = TRUE)

Arguments

range

Optional integer vector of specific seed values to search. If provided, only these seeds will be tested instead of systematic range searching.

fallback_seed

Integer seed value to return if no matching seed is found during the search process (default: 42).

max_search

Maximum seed value to search up to when performing systematic range searching. Must be a positive integer within the valid range for R's random number generator (default: 2147483647).

step_size

Step size for systematic range searching when no specific range is provided. Larger values speed up search but may miss the target seed if it falls between steps (default: 50000).

use_cpp

Logical; if TRUE, uses the fast C++ implementation via get_seed_cpp(). If FALSE, falls back to the slower R implementation with a warning (default: TRUE).

batch_size

Integer specifying the number of seeds to process in each C++ batch operation. Larger batches are more memory efficient but require more RAM. Only used in get_seed_cpp() (default: 10000).

verbose

Logical; if TRUE, prints progress information during the search process including batch progress and timing information. Only used in get_seed_cpp() (default: TRUE).

...

Additional arguments passed to get_seed_cpp() when use_cpp = TRUE.

Details

The functions work by systematically testing seed values to find one that reproduces the current RNG state stored in .Random.seed. The search process:

  • Tests each candidate seed by setting it and comparing the resulting RNG state

  • Uses efficient C++ implementation for faster processing of large search spaces

  • Supports both targeted searching (via range parameter) and systematic range searching

  • Employs batched processing to optimize memory usage and performance

Performance Considerations:

The C++ implementation (get_seed_cpp()) provides significant performance improvements:

  • Batch processing reduces overhead for large search spaces

  • Optimized memory management prevents excessive RAM usage

  • Native C++ random number generation matching R's implementation

  • Progress reporting for long-running searches

Search Strategy:

  • If range is provided: Tests only the specified seed values

  • If range is NULL: Performs systematic search from 1 to max_search in steps of step_size

  • Search terminates immediately when a matching seed is found

  • Returns fallback_seed if no match is found within the search parameters

Memory Management:

The C++ implementation uses batched processing controlled by batch_size to:

  • Process large search ranges without excessive memory allocation

  • Provide regular progress updates during long searches

  • Allow interruption of long-running operations

Value

Returns an integer representing the seed value that reproduces the current random number generator state. If no matching seed is found within the search parameters, returns the fallback_seed value.

Note

  • Requires an active RNG state (i.e., .Random.seed must exist)

  • Large search ranges may take considerable time even with C++ optimization

  • The search is deterministic but computationally intensive

  • Consider using smaller step_size values if the initial search fails

See Also

set.seed, .Random.seed

Examples

## Basic seed recovery after generating random numbers
set.seed(123)
recovered_seed <- get_seed()
print(recovered_seed)


EDOtrans documentation built on May 21, 2026, 9:07 a.m.

Related to get_seed in EDOtrans...