seedling

Crates.ioseedling
lib.rsseedling
version
sourcesrc
created_at2024-12-07 13:06:53.679245+00
updated_at2025-02-21 11:03:47.4402+00
descriptionA Rust crate for hierarchical seeded pseudo-random number generators.
homepagehttps://github.com/terahlunah/seedling
repositoryhttps://github.com/terahlunah/seedling
max_upload_size
id1475587
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Florian Poncabaré (terahlunah)

documentation

README

Seedling


Crates.io docs.rs CI Checks


Seedling is a Rust crate for creating hierarchical pseudo-random number generators (PRNGs). It provides a simple way to organize RNGs into a tree-like structure, ensuring reproducible and independent random sequences.

[dependencies]
seedling = "1.1.0"

Features

  • Hierarchical PRNGs: Organize RNGs in a tree structure with independent child generators.
  • Stable Outputs: Random sequences remain stable across code changes by using consistent subseeds.
  • Flexible RNGs: Supports 32-bit (TreeRng32), 64-bit (TreeRng64), and fast (TreeRngFast) PRNGs out of the box.
  • Standard Traits: Implements RngCore and integrates with the rand ecosystem (including rand::Rng trait).

Example

// `seedling` reexports `rand_core` for convenience
use seedling::{rand_core::RngCore, TreeRng64};

// Use `rand::Rng` for a higher level api
use rand::Rng;

fn main() {
    // It is recommended to use constants for subtree indexes to prevent accidental index changes
    const FEATURE_1_INDEX: u64 = 1;
    const FEATURE_2_INDEX: u64 = 2;

    // Indexes are arbitrary and just need to be unique per `TreeRng`
    const FEATURE_2_1_INDEX: u64 = 1;

    // The root seed of the tree
    let root_seed = 42;

    // Create a new top-level RNG
    let mut root_rng = TreeRng64::new(root_seed);

    // Create a child RNG with a constant index
    let mut feature_1_rng = root_rng.child(FEATURE_1_INDEX);
    println!("Feature 1 RNG u64: {}", feature_1_rng.next_u64());

    // Create another child RNG with a different index
    let mut feature_2_rng = root_rng.child(FEATURE_2_INDEX);
    println!("Feature 2 RNG u64: {}", feature_2_rng.next_u64());

    // Children of `TreeRng` are themselves `TreeRng` and the hierarchy can grow to arbitrary depth
    let feature_2_1_rng = feature_2_rng.child(FEATURE_2_1_INDEX);
}

Provided RNG

seedling includes out of the box support for the rand_pcg generators via the following type aliases.

  • TreeRng32: A 32-bit RNG based on Pcg32.
  • TreeRng64: A 64-bit RNG based on Pcg64.
  • TreeRngFast: A fast RNG based on Pcg64Mcg.

seedling is also easily extensible and TreeRng<T> will work with any type implementing RngCore and SeedableRng from rand_core.

Commit count: 3

cargo fmt