Skip to content

Add functions to simplify creating sub-RNGs#3775

Merged
addisoncrump merged 9 commits intoAFLplusplus:mainfrom
hjooh:sub-rng
Apr 2, 2026
Merged

Add functions to simplify creating sub-RNGs#3775
addisoncrump merged 9 commits intoAFLplusplus:mainfrom
hjooh:sub-rng

Conversation

@hjooh
Copy link
Copy Markdown
Contributor

@hjooh hjooh commented Mar 31, 2026

Addresses #3774. I updated the Rand trait to have a method (create_sub_rng) that creates a sub-RNG seeded from the current RNG state and added a simple method (sub_rand) to the HasRand trait for the sake of convenience when calling this function.

I also made a small update to MIGRATION.md (after reading through CONTRIBUTION.md) and added a function to test my changes. As this is my first attempt at contributing to this repository, I would really appreciate any and all feedback. Thank you!

Comment thread crates/fast_rands/src/lib.rs Outdated
Comment thread crates/libafl/src/state/mod.rs Outdated
Comment thread MIGRATION.md Outdated
Comment thread crates/fast_rands/src/lib.rs Outdated
@hjooh hjooh requested a review from addisoncrump March 31, 2026 14:26
Copy link
Copy Markdown
Member

@addisoncrump addisoncrump left a comment

Choose a reason for hiding this comment

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

Sorry to nitpick, but there are some design issues to address here.

Comment thread crates/fast_rands/src/lib.rs
Comment thread crates/fast_rands/src/lib.rs Outdated
@hjooh
Copy link
Copy Markdown
Contributor Author

hjooh commented Apr 1, 2026

Just to follow up: I've made the requested changes, but had some thoughts about the best way to go about it. My code is as follows:

/// Produce a sub-RNG seeded from the current RNG state.
/// Useful when sampling random data while also accessing the state.
pub trait SubRng {
    /// Creates and returns a sub-RNG.
    fn sub_rng(&mut self) -> Self;
}

impl<R: Rand + Sized + Clone> SubRng for R {
    /// Creates and returns a sub-RNG.
    fn sub_rng(&mut self) -> Self {
        let mut sub = self.clone();
        sub.set_seed(self.next());
        sub
    }
}

SubRng no longer has Rand + Sized + Clone, and I've removed the default implementation from the trait body and moved the logic to the impl block. I noticed that there were multiple RNGs (StdRand, Sfc64Rand, etc.) so I assumed that this would be the best approach, but I'm happy to change things if needed. Thank you again for the feedback!

@addisoncrump addisoncrump enabled auto-merge (rebase) April 2, 2026 08:22
@addisoncrump addisoncrump self-requested a review April 2, 2026 08:22
Copy link
Copy Markdown
Member

@addisoncrump addisoncrump left a comment

Choose a reason for hiding this comment

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

I made some final changes to clean up some lints + use preferred formatting.

@addisoncrump addisoncrump merged commit 53281a7 into AFLplusplus:main Apr 2, 2026
71 of 89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants