Skip to content

Commit

Permalink
feat(vrd): 🌱 Add MersenneTwisterConfig struct and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 4, 2024
1 parent 0d84b82 commit 106607e
Show file tree
Hide file tree
Showing 2 changed files with 420 additions and 1 deletion.
69 changes: 68 additions & 1 deletion src/mersenne_twister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,32 @@ use std::fmt;
pub struct MersenneTwisterConfig {
/// The number of elements in the array used for the Mersenne Twister algorithm.
/// Its value is set to 624 for optimal performance.
/// Must be at least 1.
pub n: usize,

/// The number of elements to skip in the array used for the Mersenne Twister algorithm.
/// Its value is set to 397 for optimal performance.
/// Must be at least 1 and less than `n`.
pub m: usize,

/// A constant value used in the Mersenne Twister algorithm.
/// Must have its highest bit set.
pub matrix_a: u32,

/// A constant value used in the Mersenne Twister algorithm.
/// Must be a valid 32-bit unsigned integer.
pub upper_mask: u32,

/// A constant value used in the Mersenne Twister algorithm.
/// Must be a valid 32-bit unsigned integer.
pub lower_mask: u32,

/// A constant value used in the Mersenne Twister algorithm.
/// Must be a valid 32-bit unsigned integer.

Check warning on line 46 in src/mersenne_twister.rs

View check run for this annotation

Codecov / codecov/patch

src/mersenne_twister.rs#L46

Added line #L46 was not covered by tests
pub tempering_mask_b: u32,

/// A constant value used in the Mersenne Twister algorithm.
/// Must be a valid 32-bit unsigned integer.
pub tempering_mask_c: u32,
}

Expand Down Expand Up @@ -106,12 +113,72 @@ impl MersenneTwisterConfig {
}
}

/// Sets all the fields of the `MersenneTwisterConfig` struct at once.
///
/// # Arguments
///
/// * `n` - The number of elements in the array.
/// * `m` - The number of elements to skip.
/// * `matrix_a` - Constant value used in the algorithm.
/// * `upper_mask` - Constant value used in the algorithm.
/// * `lower_mask` - Constant value used in the algorithm.
/// * `tempering_mask_b` - Constant value used in the algorithm.
/// * `tempering_mask_c` - Constant value used in the algorithm.
///
/// # Panics
///
/// This function panics if any of the provided parameters are outside of their valid range.
///
/// # Example
///
/// ```
/// use vrd::mersenne_twister::MersenneTwisterConfig;
///
/// let mut config = MersenneTwisterConfig::new();
/// config.set_config(
/// 1000, // n
/// 500, // m
/// 0x9908b0df, // matrix_a
/// 0x80000000, // upper_mask
/// 0x7fffffff, // lower_mask
/// 0x9d2c5680, // tempering_mask_b
/// 0xefc60000 // tempering_mask_c
/// );
/// ```
pub fn set_config(

Check failure on line 148 in src/mersenne_twister.rs

View workflow job for this annotation

GitHub Actions / Lint

this function has too many arguments (8/7)
&mut self,
n: usize,
m: usize,
matrix_a: u32,
upper_mask: u32,
lower_mask: u32,
tempering_mask_b: u32,
tempering_mask_c: u32,
) {
MersenneTwisterConfig::validate(
n,
m,
matrix_a,
upper_mask,
lower_mask,
tempering_mask_b,
tempering_mask_c,
);
self.n = n;
self.m = m;
self.matrix_a = matrix_a;
self.upper_mask = upper_mask;
self.lower_mask = lower_mask;
self.tempering_mask_b = tempering_mask_b;
self.tempering_mask_c = tempering_mask_c;
}

/// Validates the parameters for a MersenneTwisterConfig.
///
/// # Panics
///
/// This function panics if any of the provided parameters are outside of their valid range.
fn validate(
pub fn validate(
n: usize,
m: usize,
matrix_a: u32,
Expand Down
Loading

0 comments on commit 106607e

Please sign in to comment.