Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Sep 1, 2023
1 parent 7162af0 commit 5321c6a
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,64 @@ pub use bounded_rand::*;
/// A trait for pseudo-random number generators with 32-bit output per step.
pub trait Gen32 {
/// Makes the generator create the next output.
///
/// All `u32` values should have equal chance of occuring.
fn next_u32(&mut self) -> u32;

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

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

/// Gives an `f32` in `0.0 ..= 1.0`.
/// Gives a value 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`
/// Gives a value in the range `1 ..= 4`
#[inline]
fn d4(&mut self) -> i32 {
let base = self.next_u32() >> 30;
base as i32 + 1
}

/// Gives a value in the range `1..=6`
/// 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`
/// Gives a value in the range `1 ..= 8`
#[inline]
fn d8(&mut self) -> i32 {
let base = self.next_u32() >> 29;
base as i32 + 1
}

/// Gives a value in the range `1..=10`
/// 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`
/// 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`
/// 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);
Expand Down

0 comments on commit 5321c6a

Please sign in to comment.