BurntSushi / BurntSushi/quickcheck

Cannot use Rng methods on `Gen` when implementing `Arbitrary`

Open
#279 6 comments 7 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
2.8k
Forks
165
PR merge metrics
No merged PRs in 30d

Description

I noticed when trying to update a project to `quickcheck` 1.x, that [`Gen`] is now an opaque struct and users implementing `Arbitrary` can only use `Gen::choose` to get randomness. This isn't sufficient in some usecases, which were previously served by using `Gen`'s `Rng` methods: for instance, in [uutils' `factor`] I need to draw integers from large ranges known at runtime, to generate integers of known factorization:

```rust
impl quickcheck::Arbitrary for Factors {
fn arbitrary(gen: &mut quickcheck::Gen) -> Self {
use rand::Rng;
let mut f = Factors::one();
let mut g = 1u64;
let mut n = u64::MAX;

// Adam Kalai's algorithm for generating uniformly-distributed
// integers and their factorization.
//
// See Generating Random Factored Numbers, Easily, J. Cryptology (2003)
'attempt: loop {
while n > 1 {
n = gen.gen_range(1, n);
if miller_rabin::is_prime(n) {
if let Some(h) = g.checked_mul(n) {
f.push(n);
g = h;
} else {
// We are overflowing u64, retry
continue 'attempt;
}
}
}

return f;
}
}
}
```

(Technically, I *could* repeatedly use `gen.choose(&[0, 1])` to draw individual, random bits, but this would be beyond silly)

[`Gen`]: https://docs.rs/quickcheck/1.0.3/quickcheck/struct.Gen.html
[uutils' `factor`]: https://github.com/uutils/coreutils/blob/7942a64/src/uu/factor/src/factor.rs#L292-L321

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the Gen API described in the issue and the linked quickcheck documentation, then review the existing Arbitrary integration points. The change is complete when Arbitrary implementations can draw random values from runtime-sized ranges without repeatedly generating individual bits, with behavior matching the requested use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.