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.

Efficiently Generating a Number in a Range

74 pointsby pettoualmost 7 years ago

7 comments

dagenixalmost 7 years ago
&gt; Let&#x27;s move from an over-engineered approach to an under-engineered one.<p>The article says this to deride C++s implementation as being too complicated because it supports ranges such as [-3,17] and then promptly goes on to discuss how a modulo based implementation is very biased if the upper end of the range is above 2^31. It&#x27;s not really clear why the former use case is unimportant but the latter isn&#x27;t.<p>It just goes to show that one person&#x27;s niche use case is another person&#x27;s main use case. I wish people would just avoid the judgemental term &quot;over engineered&quot; and instead focus on matching appropriate algorithms to appropriate use cases.
评论 #17610626 未加载
评论 #17610551 未加载
rootlocusalmost 7 years ago
<p><pre><code> return min + (max - min) &#x2F; 2 </code></pre> Oh, you want a random number?
评论 #17614702 未加载
kazinatoralmost 7 years ago
In TXR Lisp, the algorithm I put in place basically finds the tightest power-of-two bounding box for the modulus, clisp the pseudo-andom number to that power-of-two range, and then rejects values outside of the modulus.<p>Example: suppose we wanted values in the range 0 to 11. The tightest power of two is 16, so we generate 4 bit pseudo-random numbers in the 0 to 15 range. If we get a value in the 12 to 15 range, we throw it away and choose another one.<p>The clipping to the power-of-two bounding box ensures that we reject at most 50% of the raw values.<p>I don&#x27;t bother optimizing for small cases. That is, under this 4 bit example, each generated value that is trimmed to 4 bits will be the full output of the PRNG, a 32 bit value. The approach pays off for bignums; the PRNG is called enough times to cover the bits, clipped to the power-of-two box, then subject to the rejection test.
评论 #17612922 未加载
评论 #17612553 未加载
nerdponxalmost 7 years ago
This is well-timed. I don&#x27;t know much about different random number generators but I do know that we recently had an problem where RNG was a serious performance bottleneck.
评论 #17610542 未加载
评论 #17610571 未加载
评论 #17611751 未加载
adrianmonkalmost 7 years ago
I wonder if the &quot;Bitmask with Rejection&quot; method would be more efficient if you sometimes made the mask one bit larger than strictly necessary.<p>As it is, if you want a number in the range 0..8, you take 4 bits of randomness, giving you a number in 0..15. This is great, but 7&#x2F;16 (43.75%) of the time you have to try again. This not only means more loop iterations, it also means you discard 4 bits of randomness, which may have been costly to generate.<p>If instead you took 5 bits of randomness, you&#x27;d be able to accept anything in 0..26 and would only have to reject 27..31, which means only rejecting 5&#x2F;32 (15.625%) of the time.<p>0..8 is a particularly bad case, though. If you need numbers in the range 0..14, then it&#x27;s not worth trying to use 5 bits.
modelessalmost 7 years ago
It seems crazy to me that there&#x27;s no way to produce unbiased numbers in an arbitrary range without rejection sampling and a loop. Is there a proof of this?
评论 #17611569 未加载
评论 #17611105 未加载
评论 #17611429 未加载
评论 #17613169 未加载
sdmike1almost 7 years ago
Personally I&#x27;m a fan of the xoshiro[1] generator I have found it to be faster and give more equiprobable outputs.<p>[1]<a href="http:&#x2F;&#x2F;xoshiro.di.unimi.it" rel="nofollow">http:&#x2F;&#x2F;xoshiro.di.unimi.it</a>
评论 #17609614 未加载
评论 #17610014 未加载
评论 #17610117 未加载