HalfCauchy

Struct HalfCauchy 

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

Half Cauchy Distribution

§Example

// Create a new Half-Cauchy distribution with a scale parameter of 1
let mut half_cauchy = rand_simple::HalfCauchy::new(1192_u32);
// Ensure that the distribution's representation includes the scale parameter
assert_eq!(format!("{half_cauchy}"), "HCa(Scale parameter) = HCa(1)");
// Generate a random number following the Half-Cauchy distribution with the initial scale parameter
println!("Returns a random number -> {}", half_cauchy.sample());

// Modify the distribution's scale parameter
let scale: f64 = 1.5;
// Update the scale parameter and generate a random number following the modified Half-Cauchy distribution
let result: Result<f64, &str> = half_cauchy.try_set_params(scale);
// Ensure that the distribution's representation includes the updated scale parameter
assert_eq!(format!("{half_cauchy}"), "HCa(Scale parameter) = HCa(1.5)");
println!("Returns a random number -> {}", half_cauchy.sample());

Implementations§

Source§

impl HalfCauchy

Source

pub fn new(_seed: u32) -> Self

Constructor

Create a new instance of the HalfCauchy distribution with a specific random seed. The random seed ensures that different instances are not initialized with the same values.

§Arguments
  • _seed - A random seed value for initializing the generator.
§Returns

A new instance of the HalfCauchy distribution.

Source

pub fn sample(&mut self) -> f64

Calculate a random number.

This function generates a random number following the Cauchy distribution. It uses the Marsaglia polar method to obtain a random number with a Cauchy distribution. The generated random number is scaled by the scale parameter.

§Returns
  • f64 - A random number following the Cauchy distribution scaled by the scale parameter.
Source

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

Attempt to change the parameters of the random variable.

  • scale - Scale parameter

If the provided scale is less than or equal to zero, an error is returned. In such cases, the parameters of the random variable remain unchanged.

§Arguments
  • scale - The new scale parameter to set for the random variable.
§Returns
  • Result<f64, &str> - A Result indicating success with the new scale value or an error message.

Trait Implementations§

Source§

impl Display for HalfCauchy

Source§

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

Formatter for displaying in println! and similar macros.

  • Scale parameter

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.