Absolucy / Absolucy/nanorand-rs

Generated floats are not uniformly distributed

Ouverte
#30 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
bug good first issue
Langage dominant
Rust
Étoiles
251
Forks
24
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

Hej 👋

The documentation does not say anything about the distribution of the values, though one would assume that primitive values ought to be uniformly distributed. This is not the case for f32 and f64 based on https://github.com/Absolucy/nanorand-rs/blob/5e4ea401102cb3e1a519fed84778bc110d792e56/nanorand/src/gen.rs#L97 and https://github.com/Absolucy/nanorand-rs/blob/5e4ea401102cb3e1a519fed84778bc110d792e56/nanorand/src/gen.rs#L103

Floats above a certain value lose prevision, so using, say, `u32::MAX` and `u32::MAX - 42` as the numerator both yield `1.0`, which leads to a bias of the generated floats towards 1.0. This playground illustrates it a bit more: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=465eb2dcf33851195dde9f3a6601ea22

If you want to fix that, a solution would be to use the "safe" value provided by `::MANTISSA_DIGITS` as the max instead of `::MAX`. I could ope a PR for that if you like.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

The issue points to lines 97 and 103 in nanorand/src/gen.rs where f32 and f64 generation uses u32::MAX and u64::MAX, causing bias. The fix involves using f32::MANTISSA_DIGITS and f64::MANTISSA_DIGITS to determine a safe maximum numerator. Start by examining the current implementation and the linked playground to understand the bias. Then, modify the generation logic to use the mantissa digits for uniform distribution and run existing tests to ensure correctness.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
rust
Domaine
devtools
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.