pub struct FDistribution { /* private fields */ }Expand description
F-distribution
§Usage Example
// Create a new FDistribution instance with the provided seeds.
// These seeds are used to initialize the internal state variables necessary for the random number generation.
let mut f = rand_simple::FDistribution::new([1192_u32, 765_u32, 1543_u32, 2003_u32, 1192_u32, 765_u32, 1543_u32, 2003_u32]);
// Check that the initial degrees of freedom parameters are correctly set to (1, 1).
// This ensures that the F-distribution is initialized with the default settings.
assert_eq!(format!("{f}"), "F(Degree of Freedom parameter 1, Degree of Freedom parameter 2) = F(1, 1)");
// Generate and print a random number from the F-distribution with the initial degrees of freedom (1, 1).
// The `sample()` method is used to produce this random number.
println!("With the initial setup, generates a random number following the F-distribution with degrees of freedom (1, 1) -> {}", f.sample());
// Modify the degrees of freedom parameters.
// Here, `degree_of_freedom_1` is set to 2 and `degree_of_freedom_2` is set to 3, changing the shape of the distribution.
let degree_of_freedom_1: u64 = 2_u64; // New degrees of freedom for the first parameter.
let degree_of_freedom_2: u64 = 3_u64; // New degrees of freedom for the second parameter.
// Attempt to update the F-distribution's parameters using the new degrees of freedom.
// The `try_set_params` method returns a `Result`, allowing us to handle any potential errors during this update.
let result: Result<(u64, u64), &str> = f.try_set_params(degree_of_freedom_1, degree_of_freedom_2);
// Verify that the degrees of freedom have been successfully updated to (2, 3).
// This assertion ensures that the distribution is now set up with the new parameters.
assert_eq!(format!("{f}"), "F(Degree of Freedom parameter 1, Degree of Freedom parameter 2) = F(2, 3)");
// Generate and print a random number from the F-distribution with the updated degrees of freedom (2, 3).
// The `sample()` method now reflects the updated distribution parameters.
println!("Generates a random number with the updated degrees of freedom ({}, {}) -> {}", degree_of_freedom_1, degree_of_freedom_2, f.sample());Implementations§
Source§impl FDistribution
impl FDistribution
Sourcepub fn new(seeds: [u32; 8]) -> Self
pub fn new(seeds: [u32; 8]) -> Self
Constructor for the FDistribution struct.
This function initializes a new instance of the FDistribution with given seeds.
§Arguments
seeds- An array of 8 unsigned 32-bit integers (u32). These serve as the seeds for the random number generators that will be used within the distribution. The seeds are critical for ensuring the randomness of the generated numbers.
§Returns
Returns an instance of the FDistribution struct initialized with the provided seeds.
Sourcepub fn sample(&mut self) -> f64
pub fn sample(&mut self) -> f64
Generates a random number that follows the F-distribution.
This method implements the algorithm to compute a random variable that follows the F-distribution by generating two chi-squared distributed random variables and then computing their ratio.
§Returns
Returns a f64 value that represents a random sample from the F-distribution.
Sourcepub fn try_set_params(
&mut self,
degree_of_freedom_1: u64,
degree_of_freedom_2: u64,
) -> Result<(u64, u64), &str>
pub fn try_set_params( &mut self, degree_of_freedom_1: u64, degree_of_freedom_2: u64, ) -> Result<(u64, u64), &str>
Attempts to set new parameters for the F-distribution’s degrees of freedom. This function validates the provided degrees of freedom and updates the struct’s internal state if the validation passes. If the validation fails, it returns an error message, and the parameters remain unchanged.
§Arguments
degree_of_freedom_1- The first degree of freedom, associated with the numerator in the F-distribution.degree_of_freedom_2- The second degree of freedom, associated with the denominator in the F-distribution.
§Returns
A Result type:
Ok((u64, u64))- Returns a tuple containing the updated degrees of freedom if the parameters are valid.Err(&str)- Returns an error message if either degree of freedom is invalid (i.e., less than 1).