Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I got lost when OP talked about using 10 integers to choose from 3 choices. I think I figured out what was missing in the explanation.

random_u64() Mod 3 does indeed have a single bucket that is oversized. This overweights one option by about 5×10^-20.

rand() Itself has only 32767 possible values, so it's also common for a bucket to be overweighted depending on the number of buckets.

 help



Using 10 integers was actually a simplification to explain what you're talking about here.

If the basic available function was rand_1_to_10(), modulo-ing its result by 3 would clearly make one result more probable than the others. The same happens with random_u64(); even though the difference is smaller, it can still be significant.


Assuming the code is black box, an outside observer would need no more than a thousand observations before they could definitely say something was off about your rand_1_to_10() mod 3 code (with 95% confidence).

But in the case of the random_u64() mod 10, you would need the heat death of the universe number of samples before that bias would be noticeably distinguishable from random.

Is there an actual example of where that specific case could be significant for anything other than maybe nation-state cryptography?


Makes you wonder at what point overweighting by about 5×10^-20 is something you'd want to care about.

this is what has always baffled me about statistics...having the opportunity to make things exact, with little effort, it is dismissed just because the small error

It's basic engineering to do what is necessary for solving a problem. Not more.

Not unless of your definition for "what is neccessary for solving a problem" already includes tolerances for the unforseen.

Picking that from the noise would take quite a while.

> rand() Itself has only 32767 possible values

For MingW maybe (due to MSVCRT). I think most libraries such as glibc have RAND_MAX at 2147483647.


That sounds related to the pigeon hole principle: if you have n objects and m buckets, and m doesn’t divide n, then some buckets have more objects than others.

https://en.wikipedia.org/wiki/Pigeonhole_principle




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: