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
impl HalfCauchy
Sourcepub fn new(_seed: u32) -> Self
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.
Sourcepub fn sample(&mut self) -> f64
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 thescaleparameter.
Sourcepub fn try_set_params(&mut self, scale: f64) -> Result<f64, &'static str>
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>- AResultindicating success with the new scale value or an error message.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for HalfCauchy
impl RefUnwindSafe for HalfCauchy
impl Send for HalfCauchy
impl Sync for HalfCauchy
impl Unpin for HalfCauchy
impl UnwindSafe for HalfCauchy
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