Absolucy / Absolucy/nanorand-rs

Generated floats are not uniformly distributed

Aperta
#30 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug good first issue
Lingua principale
Rust
Stelle
251
Fork
24
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
rust
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.