Skip to content

Unit circle distribution #665

@dhardy

Description

@dhardy

@vks How come we never noticed in #567 that the second term is always positive?

[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 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):

      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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions