rng-pack

Crates.iorng-pack
lib.rsrng-pack
version0.4.0
created_at2026-01-16 06:47:56.250732+00
updated_at2026-01-24 17:55:05.115357+00
descriptionRandom number generator variety pack
homepage
repository
max_upload_size
id2048027
size6,103,612
(cet-t)

documentation

README

RNG Pack

A collection of pseudo-random number generators (PRNGs) implemented in Rust.

This crate provides efficient implementations of various random number generation algorithms, supporting both Rust and C (via FFI).

Supported Generators

  • Mersenne Twister:
    • mt19937::Mt19937 (32-bit)
    • mt1993764::Mt1993764 (64-bit)
  • PCG:
    • pcg32::Pcg32 (Permuted Congruential Generator, PCG-XSH-RR)
  • Philox (Counter-based RNGs):
    • philox32::Philox32 (4x32)
    • philox64::Philox64 (2x64)
  • Twisted GFSR:
    • twisted_gfsr::TwistedGFSR
  • Xorshift:
    • xorshift32::Xorshift32
    • xorshift64::Xorshift64
    • xorshift128::Xorshift128

Installation

Add this to your Cargo.toml:

[dependencies]
rng_pack = "0.1.0"

Usage Examples

Basic Usage (Mersenne Twister)

use rng_pack::mt19937::Mt19937;

fn main() {
    // Initialize with a seed
    let mut rng = Mt19937::new(12345);

    // Generate a random u32
    let val = rng.nextu();
    println!("Random u32: {}", val);

    // Generate a random float in [0, 1)
    let f_val = rng.nextf();
    println!("Random f32: {}", f_val);

    // Generate a random integer in a range [min, max]
    let range_val = rng.randi(1, 100);
    println!("Random integer (1-100): {}", range_val);
}

Using Philox (Counter-based)

Philox generators produce blocks of random numbers and are suitable for parallel applications.

use rng_pack::philox32::Philox32;

fn main() {
    let mut rng = Philox32::new([123, 456]);
    
    // Generates 4 x u32 values at once
    let values = rng.nextu();
    println!("Random values: {:?}", values);
}

C API

This crate exports C-compatible functions for all generators, allowing them to be used as a shared library.

Each generator exposes functions for:

  • Creation (_new)
  • Destruction (_free)
  • Bulk generation (_next_uXXs, _next_fXXs)
  • Range generation (_rand_iXXs, _rand_fXXs)

Example signature for MT19937:

void* mt19937_new(uint32_t seed);
void mt19937_next_u32s(void* ptr, uint32_t* out, size_t count);
void mt19937_free(void* ptr);
Commit count: 0

cargo fmt