logoalt Hacker News

deathanatos • today at 3:38 AM • 2 replies • view on HN

If your RNG outputs a u64 (a getrandom() call can do this, or even a simple RNG like xoshiro), a random int in TFA's range of [0, 10) has a very small (6 in 2⁶⁴ chance, or 3.2e-17%, might as well be 0) chance of needing to redraw.

Obviously, wider ranges will (possibly) reject more often, but unless the range is huge, rejection should be a very cold branch.


Replies

skybrian • today at 7:58 AM

You’re throwing a lot of bits away, though. If you used four bits per number then you could pack 16 random digits into one 64-bit draw. But a rejection-sampling branch will be taken much more often.

➕ show 1 reply