This is a small PRNG library/framwork written in pure Rust, that is a translation of another project of mine, SmallPRNG. The main goal of this project is to not implement every feature possible but to provide a general framework for implmenting PRNG algorithms to test monte carlo codes. This was made primarilly as a educational project of learning Rust and it's features but I hope that this can be used for productive projects like SmallPRNG was.
To live up to the name of SmolPRNG
there are less then 1000 lines of code but implements over 22 different algorithms out of the box, can sample from 15 statistical distributions this includes all code + tests + docs + benchs.
SmolPRNG is performance competative to the Rand Rust crate and is much more straightforward to extend.
f32
,f64
Generating random numbers is straight forward after initilizing a PRNG
object
```rust let prng = PRNG{generator: JsfGenerator::default()};
let randbool = prng.genbool(); // Generates a random bool
let randu8 = prng.genu8(); //Generates a random u8 let randu16 = prng.genu16(); //Generates a random u16 let randu32 = prng.genu32(); //Generates a random u32 let randu64 = prng.genu64(); //Generates a random u64 let randu128 = prng.genu128(); //Generates a random u128
let randf32 = prng.genf32(); //Generates a random f32 let randf64 = prng.genf64(); //Generates a random f64 ```
Here is an example of injecting a new algorithm to generate pseudo-random nunmbers by impl
the Algorithm
trait on a struct. Availible Outputs
are u8
,u16
,u32
,u64
,u128
.
```rust struct StepGenerator{ state: u32, }
impl Algorithm for StepGenerator { type Output = u32;
fn gen(&mut self) -> Self::Output { self.data = self.data.overflowing_add(1).0; self.data } }
// somewhat gross macro, that adds the traits Iterator, Default, and From where U in {u8, u16, u32, u64, u128} prngsetup! {StepGenerator, StepGenerator, data, make1_u32} ```
Using this, we can then create a PRNG
struct from
rust
let gen_state = 12765u32;
let prng = PRNG{generator: StepGenerator{data: gen_state}}