There's another approach for doing this: generate a random number between 1 and 2 (they all have the same exponent) and then subtract 1 (that's the default implementation in Julia [0]).
But I think it also just gives you 2^53 different numbers.<p>So, between .5 and 1 you miss out on every second representable number, between 0.25 and .5 you miss out on 3/4 of them, and so on.<p>I guess for many cases that's good enough, but the article seems like a nice improvement.<p>ETA: Lemire has some thoughts on this [1] and links to what might be a prior solution [2]. Vigna (of xoroshiro fame) writes about it at the bottom of [3] and also links to [2]. So, presumably the implementation described in the article is faster? ("There have been some past attempts to fix these flaws, but none that avoid a huge performance penalty while doing so."<p>EDIT2: BTW, one of the things I love about HN (well, the world, really) is that there are people that care deeply that we can uniformly sample floats between 0 and 1 correctly, and all of them, and do it faster.<p>[0] see <a href="https://github.com/JuliaLang/julia/blob/master/stdlib/Random/src/generation.jl">https://github.com/JuliaLang/julia/blob/master/stdlib/Random...</a><p><pre><code> rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01_64})
= rand(r, CloseOpen12()) - 1.0
</code></pre>
[1] <a href="https://lemire.me/blog/2017/02/28/how-many-floating-point-numbers-are-in-the-interval-01/" rel="nofollow">https://lemire.me/blog/2017/02/28/how-many-floating-point-nu...</a><p>[2] <a href="https://mumble.net/~campbell/2014/04/28/uniform-random-float" rel="nofollow">https://mumble.net/~campbell/2014/04/28/uniform-random-float</a><p><a href="https://mumble.net/~campbell/2014/04/28/random_real.c" rel="nofollow">https://mumble.net/~campbell/2014/04/28/random_real.c</a><p>[3] <a href="https://prng.di.unimi.it" rel="nofollow">https://prng.di.unimi.it</a>