rust-random / rust-random/rand_distr

Infinite loop in Hypergeometric::sample with large (>2^50) parameters

Open
#35 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
26
Forks
16
PR merge metrics
No merged PRs in 30d

Description

Summary

With large parameters, sampling from the Hypergeometric distribution implementation can hang in an infinite or very slow loop somewhere. I'd prefer success or an explicit error.

This is probably a consequence of limited precision floating point -- the inputs that trigger this are inching toward the 2^53 boundary . I haven't looked into the cause in detail yet, and will update when I have time to find out more. (Edit: this may be a fundamental issue with large parameters passed to algorithm "HIN", making it calculate a ratio of binomials incredibly slowly.)

Code sample

use rand::SeedableRng;
use rand::rngs::Xoshiro128PlusPlus;
use rand_distr::{Distribution, Hypergeometric};

fn main() {
    let mut rng = Xoshiro128PlusPlus::seed_from_u64(0x1);
    let dist = Hypergeometric::new(5277655813324800, 527765581332480, 52776558133248).unwrap();
    println!("{}", dist.sample(&mut rng));
}

Notes

While I encountered this while trying to write some additional property tests, it's possible to systematically look for hang and crash issues of this type with a fuzzer. As testing methods go, fuzzing is somewhat awkward and can require a lot of manual work to explore a complicated system; but most of the rand_distr sampler code doesn't have too complicated control flow or internal checks. My current fuzzer script is in https://github.com/mstoeckl/rand_distr/commits/fuzz-params/ ; if you'd like I can make a pull request out of it (or start reporting issues that come from it, or just PRs to fix them -- there are a few more distributions that can crash or hang on unusual inputs.)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Hypergeometric::sample and reproduce the provided large-parameter example. Inspect algorithm "HIN" and its handling of ratios with parameters near the 2^53 boundary. Done means sampling terminates reliably, either returning a result or an explicit error, with regression coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.