Triangular

Struct Triangular 

Source
pub struct Triangular { /* private fields */ }
Expand description

Represents a triangular distribution defined by a minimum value, maximum value, and mode.

§Example Usage

let mut triangular = rand_simple::Triangular::new(1192_u32);

// Default configuration
assert_eq!(format!("{triangular}"), "TRI(Min, Max, Mode) = TRI(0, 1, 0.5)");
println!(
    "Generates a random number following a triangular distribution over [0, 1] with mode 0.5 -> {}",
    triangular.sample()
);

// Modify distribution parameters
let min: f64 = -1_f64;
let max: f64 = 1_f64;
let mode: f64 = 0.25_f64;
let result: Result<(f64, f64, f64), &str> = triangular.try_set_params(min, max, mode);

assert_eq!(format!("{triangular}"), "TRI(Min, Max, Mode) = TRI(-1, 1, 0.25)");
println!(
    "Generates a random number following a triangular distribution over [{}, {}] with mode {} -> {}",
    min, max, mode, triangular.sample()
);

§Fields

  • xyzuv: A state variable used for random number generation.
  • min: The minimum value of the distribution, defining the left endpoint of the interval.
  • max: The maximum value of the distribution, defining the right endpoint of the interval.
  • mode: The mode (peak) of the distribution, indicating the value where the distribution reaches its maximum density.

Implementations§

Source§

impl Triangular

Source

pub fn new(_seed: u32) -> Self

Constructor for the Triangular struct. Initializes the random number generator state and sets default parameters.

§Arguments
  • _seed - A seed value used to initialize the random number generator state.
§Default Parameters
  • min: 0.0 (lower bound of the interval)
  • max: 1.0 (upper bound of the interval)
  • mode: 0.5 (most likely value within the interval)
Source

pub fn sample(&mut self) -> f64

Generates a random number following the triangular distribution.

This method uses Algorithm 3.95 (Inverse Transform Sampling) to generate a random value based on the current min, max, and mode parameters.

§Returns

A random number following the triangular distribution.

Source

pub fn try_set_params( &mut self, min: f64, max: f64, mode: f64, ) -> Result<(f64, f64, f64), &str>

Updates the parameters of the triangular distribution.

§Arguments
  • min - The minimum value of the distribution.
  • max - The maximum value of the distribution.
  • mode - The mode (most probable value) of the distribution.
§Validation
  • The min must be less than the max.
  • The mode must lie within the interval [min, max].
§Returns
  • Ok((min, max, mode)) if the parameters are valid.
  • Err(&str) if the parameters are invalid, with an error message explaining the issue.

Trait Implementations§

Source§

impl Display for Triangular

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formatter for displaying with println! macro and others.

  • Range (Closed Interval)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.