-
-
Notifications
You must be signed in to change notification settings - Fork 481
Description
@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
| [diff / sum, 2.*x1*x2 / sum] |
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
f64from au64we are wasting a few bits which we could save as in thezigguratfunction) -
use the trig identities twice; this is a bit messy and yields the following by my calculation (
r**2 = x1**2 + x2**2):cos(4θ) = (x1**4 + x2**4 - 6 * x1**2 * x2**2) / r**4 sin(4θ) = 4*(x1**3 * x2 - x1 * x2**3) / r**4 -
start instead from the upper half-circle by taking
x2 = 2 * sample - 1