Erlang

Struct Erlang 

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

Erlang Distribution

Represents a random variable following the Erlang distribution.

§Examples

// Create a new Erlang distribution with specified seeds
let mut erlang = rand_simple::Erlang::new([1192u32, 765u32, 1543u32]);

// Ensure the initial parameters are correctly set
assert_eq!(format!("{erlang}"), "Er(Shape parameter, Scale parameter) = Er(1, 1)");

// Generate a random number from the Erlang distribution
println!("Generating a random number following the standard Erlang distribution with shape parameter r = 1 and scale parameter θ = 1 -> {}", erlang.sample());

// Modify the parameters of the random variable
let shape: i64 = 2_i64;
let scale: f64 = 1.5_f64;
let result: Result<(i64, f64), &str> = erlang.try_set_params(shape, scale);

// Ensure the parameters are updated correctly
assert_eq!(format!("{erlang}"), "Er(Shape parameter, Scale parameter) = Er(2, 1.5)");

// Generate a random number from the modified Erlang distribution
println!("Generating a random number following the Erlang distribution with shape parameter r = {} and scale parameter θ = {} -> {}", shape, scale, erlang.sample());

Implementations§

Source§

impl Erlang

Source

pub fn new(seeds: [u32; 3]) -> Self

Constructor

Creates a new instance of the Erlang distribution with the provided seeds for random number generation. Seeds are adjusted internally to ensure uniqueness.

§Arguments
  • seeds - Seeds for random number generation. Adjusted internally to ensure uniqueness.
§Returns

A new instance of the Erlang distribution.

§Example
// Create a new Erlang distribution with specified seeds
let mut erlang = rand_simple::Erlang::new([1192u32, 765u32, 1543u32]);
Source

pub fn sample(&mut self) -> f64

Generate a random number.

This method calculates a random number using the standard gamma distribution.

§Returns

Returns a random number generated from the standard gamma distribution, multiplied by the scale parameter.

§Examples
let mut erlang = rand_simple::Erlang::new([1192u32, 765u32, 1543u32]);
let random_number = erlang.sample();
println!("Random number: {}", random_number);
Source

pub fn try_set_params( &mut self, shape: i64, scale: f64, ) -> Result<(i64, f64), &str>

Attempt to modify the parameters of the random variable.

  • shape - Shape parameter.
  • scale - Scale parameter.
§Arguments
  • shape - Shape parameter.
  • scale - Scale parameter.
§Returns

Returns a tuple (i64, f64) representing the modified parameters (shape, scale).

§Errors

Returns an error message if the provided parameters are invalid.

§Examples
let mut erlang = rand_simple::Erlang::new([1192u32, 765u32, 1543u32]);
assert_eq!(erlang.try_set_params(3, 0.5), Ok((3, 0.5)));
assert_eq!(erlang.try_set_params(0, 0.5), Err("The shape parameter is less than or equal to 0. The parameters of the random variable will remain unchanged."));
assert_eq!(erlang.try_set_params(1, -0.1), Err("The scale parameter is less than or equal to 0. The parameters of the random variable will remain unchanged."));

Trait Implementations§

Source§

impl Display for Erlang

Source§

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

Formatter for displaying in macros like print!

  • Shape parameter
  • Scale parameter

Auto Trait Implementations§

§

impl Freeze for Erlang

§

impl RefUnwindSafe for Erlang

§

impl Send for Erlang

§

impl Sync for Erlang

§

impl Unpin for Erlang

§

impl UnwindSafe for Erlang

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.