|
|
|
|
|
by camel-cdr
1221 days ago
|
|
Yeah, the amount of times I've seen `rand() % range` and other non-uniform distributions is staggering. I've actually held a presentation on the topic, with a chapter on generating uniform floats [1] There I also describe a simplified algorithm for generating uniform floats where every representable floating point value can occur with the appropriate probability. I'm still not 100% sure that my implementation is correct, but I did all I could think of to test it [2] [1] https://youtube.com/watch?v=VHJUlRiRDCY&t=2774s [2] https://github.com/camel-cdr/cauldron/blob/main/cauldron/ran... |
|
>I'm still not 100% sure that my implementation is correct
It's super hard to do so. I've often thought of using Z3 to try to formally prove that my algorithms are correct, but have not yet done so. I had a version for about a decade then found a tiny bug and that type of stuff keeps me always thinking of how to formally derive things, but the formal proofs get hard. Z3 has a really nice proper IEEE floating point object that lets you do such things slightly less painfully.