Skip to content

Add Seeded Samplers - #537

Merged
jnsiemer merged 5 commits into
devfrom
seeded_rngs
Oct 19, 2026
Merged

jnsiemer merged 5 commits into
devfrom
seeded_rngs

Conversation

@jnsiemer

@jnsiemer jnsiemer commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Description

This PR implements...

  • optional seeding for base samplers (uniform, binomial, discrete Gaussian),
  • seeded functions for the most important user-facing APIs

Testing

  • I added basic working examples (possibly in doc-comment)
  • I added tests for large (pointer representation) values
  • I included tests for all reasonable edge cases

Checklist:

  • I have performed a self-review of my own code
    • The code provides good readability and maintainability s.t. it fulfills best practices like talking code, modularity, ...
      • The chosen implementation is not more complex than it has to be
    • My code should work as intended and no side effects occur (e.g. memory leaks)
    • The doc comments fit our style guide

@jnsiemer jnsiemer self-assigned this Oct 9, 2026
@jnsiemer jnsiemer added the enhancement📈 New feature label Oct 9, 2026
@jnsiemer
jnsiemer marked this pull request as ready for review October 12, 2026 10:07

@Marvin-Beckmann Marvin-Beckmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very elegant solution! I have a few design decisions one could consider, but the solution itself seems sound.

Comment on lines +252 to +253
#[cfg(test)]
mod test_sample_binomial_seeded {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general suggestion, or rather a design decision:

Do we want to have the tests separated in modules? Alternatively, maybe it is easier to just loop over the different sampling methods, and check something like boundary checks. This ofc does not work for static tests for seeded samplers, but it could reduce maintainable code lines.

Comment on lines +277 to +285
/// Checks whether the same seed results in the same sample.
#[test]
fn same_seed_same_sample() {
use crate::integer::MatZ;
let sample_0 = MatZ::sample_binomial_seeded(2, 2, 5, 0.5, [42; 32]).unwrap();
let sample_1 = MatZ::sample_binomial_seeded(2, 2, 5, 0.5, [42; 32]).unwrap();

assert_eq!(sample_0, sample_1);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these types of tests also be constant tests? I.e., not just test if the same seed gives the same value, but also that seeded sampling gives the same constant value?

Comment thread src/macros/seeded.rs
//! This module implements macros which are used to provide an unseeded and a
//! seeded variant of a function from one implementation and one doc-comment.

/// Takes a function `name` whose last parameter is an optional seed `seed: Option<[u8; 32]>`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The design decision as to why 32 Byte seeds are chosen is not completely clear: I would guess to have 256 bits, and it is fixed by the rng? One could otherwise consider just allowing to insert any type of Byte-Array too. I would just document the decision in the macro somewhere

sigma: impl Into<f64>,
seed: Option<[u8; 32]>,
) -> Result<MatQ, MathError> {
let mut rng = SamplerRng::new(seed);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the developer it might ne be fully clear that this is how to use the sampler black-box in the construction, at least I did not find a comment on this in the macro file

@jnsiemer
jnsiemer merged commit a093e9d into dev Oct 19, 2026
2 checks passed
@jnsiemer
jnsiemer deleted the seeded_rngs branch October 19, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement📈 New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants