Universal RNG
A collection of efficient pseudo-random number generators (PRNGs) implemented in pure Rust. This crate provides a wide variety of algorithms, ranging from standard Mersenne Twister to modern high-performance generators like Xoshiro and Philox.
Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
Supported Generators
All generators implement either the Rng32 or Rng64 trait, providing a unified interface.
32-bit Generators (urng::rng32)
Output u32 natively.
| Struct | Algorithm | Period / State | Description |
|---|---|---|---|
Mt19937 |
Mersenne Twister | $2^{19937}-1$ | Standard reliable generator. |
Pcg32 |
PCG-XSH-RR | $2^{64}$ | Fast, statistically good, small state. |
Philox32 |
Philox 4x32 | - | Counter-based, suitable for parallel use. |
Xorwow |
XORWOW | $2{192}-2{32}$ | Used in NVIDIA cuRAND. |
Xorshift32 |
Xorshift | $2^{32}-1$ | Very simple and fast. |
TwistedGFSR |
TGFSR | $2^{800}$ approx | Generalized Feedback Shift Register. |
Lcg32 |
LCG | $m$ | Linear Congruential Generator. |
64-bit Generators (urng::rng64)
Output u64 natively.
| Struct | Algorithm | Period / State | Description |
|---|---|---|---|
Xoshiro256Pp |
xoshiro256++ | $2^{256}-1$ | Recommended all-purpose generator. |
Xoshiro256Ss |
xoshiro256** | $2^{256}-1$ | Recommended all-purpose generator. |
SplitMix64 |
SplitMix64 | $2^{64}$ | Fast, used for initializing other states. |
Sfc64 |
SFC64 | $2^{256}$ approx | Small Fast Chaotic PRNG. |
Mt1993764 |
Mersenne Twister 64 | $2^{19937}-1$ | 64-bit variant of MT. |
Philox64 |
Philox 2x64 | - | Counter-based. |
Xorshift64 |
Xorshift | $2^{64}-1$ | Simple and fast. |
Cet64 |
CET | - | Custom experimental generator. |
Lcg64 |
LCG | $m$ | Linear Congruential Generator. |
Other (urng::rng128)
Xorshift128: 128-bit state Xorshift, implementsRng32(outputsu32).
Usage Examples
Basic Usage
use ;
use Rng64; // Import trait for common methods
Weighted Choice (BST)
The crate includes a binary-search-tree based weighted selector for efficient sampling.
use choice;
use Xoshiro256Pp;
Using Macros
For convenience, you can use the provided macros (must import urng::*).
These macros automatically initialize the generator with a system-seeded state.
use ; // Import macros
C API
This crate exports a C-compatible FFI generic interface. Each generator has corresponding:
_new_free_next_uXXs(bulk generation)_next_fXXs(bulk generation)
Example for Mt19937:
void* ;
void ;
void ;