sapling/eden/mononoke/revset
Yan Soares Couto 30d4c8fc2d Make RNG in 'arbitrary' calls more correct
Summary:
Using modulo on arbitrary integers to get random numbers [isn't correct](https://www.internalfb.com/diff/D31305392 (da13975a4f)?dst_version_fbid=311037904117090&transaction_fbid=550270779610744), as the distribution between numbers isn't fair (unless the size is a power of two).

This was raised on D31305392 (da13975a4f), but we decided to land that quickly to unblock builds before doing these changes.

I'm applying the changes suggested on D31305392 (da13975a4f). This is what this diff does:
- For all cases where we generate small numbers (up to 5), replace with call to `Gen::choose`, so `u32::arbitrary(g) % 3` becomes `g.choose(&[0, 1, 2]).unwrap()`.
- For generating numbers in range 0..=1, I instead replaced with generating a boolean, which gets rid of the `unreachable!` calls.
- I removed the code to generate numbers in range 0..=0.
- For generating larger numbers, I used `u64::arbitrary` instead, which should make things "less wrong".

Some things I assumed, but am happy to change before landing, just let me know:
- Theoretically we don't *need* to change the code for `% 2` and `% 4`, as the math checks out there. I changed it for consistency there, but am happy to change it back.
- Using boolean also wasn't suggested initially, I'm happy to change back.

Reviewed By: krallin

Differential Revision: D31379381

fbshipit-source-id: a0bac26ebabd32a6c65f717512de998ef5dc37c8
2021-10-06 09:39:23 -07:00
..
revset-test-helper Vendor curl-sys crate with patched curl 2021-10-04 03:13:50 -07:00
src Make RNG in 'arbitrary' calls more correct 2021-10-06 09:39:23 -07:00
Cargo.toml Vendor curl-sys crate with patched curl 2021-10-04 03:13:50 -07:00