TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The definitive guide to “modulo bias” and how to avoid it (2020)

133 pointsby isomorphover 2 years ago

12 comments

anomalroilover 2 years ago
Notice that nowadays, unlike 2 years ago, people usually recommend to use the last technique I presented there in the last paragraph before the Conclusion. Which is to generate a random value that is <i>big enough</i>, so that n-log(p) &gt; 128 so that the bias will be too small to be exploitable in practice. It&#x27;s simpler and unlike rejection sampling ensures your code cannot fall into an infinite loop in case your PRNG is broken. (I&#x27;d argue you might want your code to fail in that case anyway, but YMMV.)
评论 #32852546 未加载
rrobukefover 2 years ago
Rejection sampling moves the information leak from bias to time. As always don&#x27;t use your own cryptography in production.
评论 #32849810 未加载
LudwigNagasenaover 2 years ago
A cleaner example without bit masking: <a href="https:&#x2F;&#x2F;github.com&#x2F;openbsd&#x2F;src&#x2F;blob&#x2F;master&#x2F;lib&#x2F;libc&#x2F;crypt&#x2F;arc4random_uniform.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;openbsd&#x2F;src&#x2F;blob&#x2F;master&#x2F;lib&#x2F;libc&#x2F;crypt&#x2F;ar...</a>
评论 #32850142 未加载
xxporover 2 years ago
Everything here is focused on cryptography, but there&#x27;s other common issues that can fall into this trap too. For example, naive implementations of a hash function for a basic array + linked list hash table. If you generate a hash, and then modulo that to pick a hash bucket, you could end up with a biased distribution across the buckets, and customers complaining your performance varies wildly based on the input value.<p>Just more things to think about :)
评论 #32856993 未加载
10000truthsover 2 years ago
If you need to generate multiple such random numbers, an alternative way to resolve modulo bias with minimal entropy waste is to batch the random number generation. For example, you can generate 6 random integers in [0, 100) by generating a random integer in [0, 100^6) and performing modulo reductions on the result to get your 6 integers. 100^6 is slightly less than 256^5, so if your RNG works in units of bytes, then you can use 5 bytes to generate 6 integers in [0, 100) instead of 6 bytes.
tarakatover 2 years ago
&gt; You shouldn’t rely on these claims, because even 1 bit of bias on a 256 bit nonce value can be enough to attack certain cryptographic schemes!<p>This seems like a very high bar for a random generator to clear. It also raises a question: would using a larger nonce size actually <i>increase</i> risk, if the additional bits were biased?
评论 #32855989 未加载
评论 #32852841 未加载
barbegalover 2 years ago
The claim &quot;even 1 bit of bias on a 256 bit nonce value can be enough to attack certain cryptographic schemes&quot; is true but there have been no practical attacks against 256 bit secured schemes. And the example of the mod 107 issue only reduces the amount of bits from 6.74bits to 6.71bits (a reduction of 0.4%) so hardly worth worrying about in the real world.
nraynaudover 2 years ago
I knew the concept (every post on &quot;I want a randome number in a range&quot; mentions it), but I didn&#x27;t know the name, thanks.
astrobe_over 2 years ago
I remember very old versions of <i>man random</i> [1] warning against that sort of thing and recommending to pick the high (or low) bits of the random value. Probably the piece of advice was not correct.
评论 #32857077 未加载
eisbawover 2 years ago
(r_dist * n) &#x2F; &lt;max value of r_dist&gt; is better than r_dist % n that linear scaling should mitigate such bias from modulo bias.
评论 #32850863 未加载
exabrialover 2 years ago
Interesting. In Java, there’s random.nextInt(max). Curious if that takes this into account.
评论 #32850881 未加载
makobadoover 2 years ago
This is going to be fun at work