Crate rand_pcg

The PCG random number generators.

This is a native Rust implementation of a small selection of PCG generators. The primary goal of this crate is simple, minimal, well-tested code; in other words it is explicitly not a goal to re-implement all of PCG.

Generators

This crate provides:

These generators are all deterministic and portable (see Reproducibility in the book), with testing against reference vectors.

Seeding (construction)

Generators implement the SeedableRng trait. All methods are suitable for seeding. Some suggestions:

  1. To automatically seed with a unique seed, use SeedableRng::from_rng with a master generator (here rand::rng()):
    use rand_core::SeedableRng;
    use rand_pcg::Pcg64Mcg;
    let rng = Pcg64Mcg::from_rng(&mut rand::rng());
    # let _: Pcg64Mcg = rng;
    
  2. Seed from an integer via seed_from_u64. This uses a hash function internally to yield a (typically) good seed from any input.
    # use {rand_core::SeedableRng, rand_pcg::Pcg64Mcg};
    let rng = Pcg64Mcg::seed_from_u64(1);
    # let _: Pcg64Mcg = rng;
    

See also Seeding RNGs in the book.

Generation

Generators implement RngCore, whose methods may be used directly to generate unbounded integer or byte values.

use rand_core::{SeedableRng, RngCore};
use rand_pcg::Pcg64Mcg;

let mut rng = Pcg64Mcg::seed_from_u64(0);
let x = rng.next_u64();
assert_eq!(x, 0x5603f242407deca2);

It is often more convenient to use the rand::Rng trait, which provides further functionality. See also the Random Values chapter in the book.

Modules

Structs

Type Aliases