You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the sin(2θ) / cos(2θ) trig identities, it turns out we're sampling from the upper half-circle (x2 >= 0), since when starting from one quadrant of the circle we have angle θ in 0..π/2. We could fix this in a few ways:
use an extra bit to randomly flip the sign of the second term (note that since we're sampling f64 from a u64 we are wasting a few bits which we could save as in the ziggurat function)
use the trig identities twice; this is a bit messy and yields the following by my calculation (r**2 = x1**2 + x2**2):
Sorry, I missed that we are sampling both variables from [-1, 1). Theoretically one of them could be sampled from [0, 1) but according to my benchmark it's faster to sample both the same way.
However, I see no need to cache the Uniform distribution in UnitCircle (my bench is slightly faster without). Do you remember why you did this?
@vks How come we never noticed in #567 that the second term is always positive?
rand/src/distributions/unit_circle.rs
Line 59 in 19d3baf
Using the
sin(2θ)
/cos(2θ)
trig identities, it turns out we're sampling from the upper half-circle (x2 >= 0
), since when starting from one quadrant of the circle we have angleθ in 0..π/2
. We could fix this in a few ways:use an extra bit to randomly flip the sign of the second term (note that since we're sampling
f64
from au64
we are wasting a few bits which we could save as in theziggurat
function)use the trig identities twice; this is a bit messy and yields the following by my calculation (
r**2 = x1**2 + x2**2
):start instead from the upper half-circle by taking
x2 = 2 * sample - 1
The text was updated successfully, but these errors were encountered: