Comment

Comment by FabHK

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.

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.

I guess for many cases that's good enough, but the article seems like a nice improvement.

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."

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.

[0] see https://github.com/JuliaLang/julia/blob/master/stdlib/Random...

  rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01_64}) 
    = rand(r, CloseOpen12()) - 1.0
[1] https://lemire.me/blog/2017/02/28/how-many-floating-point-nu...

[2] https://mumble.net/~campbell/2014/04/28/uniform-random-float

https://mumble.net/~campbell/2014/04/28/random_real.c

[3] https://prng.di.unimi.it

parent FabHK 1 year ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton