Skip to content

Commit

Permalink
more default methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Aug 31, 2023
1 parent 7359d66 commit e7728eb
Showing 1 changed file with 70 additions and 3 deletions.
73 changes: 70 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@
//!
//! NOT FOR CRYPTOGRAPHIC PURPOSES.
//!
//! ## Cargo Features
//! ## Using This Crate
//!
//! * `getrandom`: This adds additional methods that use the `getrandom` crate
//! to initialize a generator from an external randomness source.
//! * Create a [PCG32] or [PCG32X] value as your generator.
//! * If you enable this crate's `getrandom` cargo feature then both types
//! will have constructor functions to seed a generator from the
//! [getrandom](getrandom::getrandom) function.
//! * Call `next_u32` on the generator to get pseudo-random `u32` values. Should
//! it matter, the higher bits of the output will be *slightly* better
//! randomness quality than the lower bits of the output.
//! * At your option, import the [Gen32] trait for various extension methods.

mod bounded_rand;
pub use bounded_rand::*;

mod pcg;
use formulas::ieee754_random_f32;
pub use pcg::*;

pub mod formulas;
Expand All @@ -26,6 +33,66 @@ pub mod formulas;
pub trait Gen32 {
/// Makes the generator create the next output.
fn next_u32(&mut self) -> u32;

/// Gives an `i32` value.
#[inline]
fn next_i32(&mut self) -> i32 {
self.next_u32() as i32
}

/// Gives a `bool` value.
#[inline]
fn next_bool(&mut self) -> bool {
(self.next_u32() as i32) < 0
}

/// Gives an `f32` in `0.0 ..= 1.0`.
#[inline]
fn next_f32_unit(&mut self) -> f32 {
ieee754_random_f32(|| self.next_u32(), false)
}

/// Gives a value in the range `1..=4`
#[inline]
fn d4(&mut self) -> i32 {
let base = BoundedRandU16::_4.sample(|| (self.next_u32() >> 16) as u16);
i32::from(base) + 1
}

/// Gives a value in the range `1..=6`
#[inline]
fn d6(&mut self) -> i32 {
let base = BoundedRandU16::_6.sample(|| (self.next_u32() >> 16) as u16);
i32::from(base) + 1
}

/// Gives a value in the range `1..=8`
#[inline]
fn d8(&mut self) -> i32 {
let base = BoundedRandU16::_8.sample(|| (self.next_u32() >> 16) as u16);
i32::from(base) + 1
}

/// Gives a value in the range `1..=10`
#[inline]
fn d10(&mut self) -> i32 {
let base = BoundedRandU16::_10.sample(|| (self.next_u32() >> 16) as u16);
i32::from(base) + 1
}

/// Gives a value in the range `1..=12`
#[inline]
fn d12(&mut self) -> i32 {
let base = BoundedRandU16::_12.sample(|| (self.next_u32() >> 16) as u16);
i32::from(base) + 1
}

/// Gives a value in the range `1..=20`
#[inline]
fn d20(&mut self) -> i32 {
let base = BoundedRandU16::_20.sample(|| (self.next_u32() >> 16) as u16);
i32::from(base) + 1
}
}

impl Gen32 for PCG32 {
Expand Down

0 comments on commit e7728eb

Please sign in to comment.