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
impl Triangular
Sourcepub fn new(_seed: u32) -> Self
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)
Sourcepub fn sample(&mut self) -> f64
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.
Sourcepub fn try_set_params(
&mut self,
min: f64,
max: f64,
mode: f64,
) -> Result<(f64, f64, f64), &str>
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
minmust be less than themax. - The
modemust 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§
Auto Trait Implementations§
impl Freeze for Triangular
impl RefUnwindSafe for Triangular
impl Send for Triangular
impl Sync for Triangular
impl Unpin for Triangular
impl UnwindSafe for Triangular
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more