Hacker News new | ask | show | jobs
by chmod775 499 days ago
Isn't there a likely faster analytical solution? At least I think sqrt, sin, and cos are generally considered to be slow.

Maybe this:

1. Generate random X [-1 to 1]

2. Based on X you can calculate a range of legal Y that will still fall within the circle. Try to use a lookup table+interpolation with desired resolution to make this really fast if there's not some sufficiently fast hardware instructions we can abuse.

3. Generate random Y in that range.

2 comments

> At least I think sqrt, sin, and cos are generally considered to be slow. […] Based on X you can calculate a range of legal Y that will still fall within the circle.

Yup, that calculation is called sqrt(1 - x²).

> Try to use a lookup table+interpolation with desired resolution to make this really fast

On modern hardware (like, most architectures from 1995-ish onwards), LUTs are mostly slower than polynomial-based solutions for the same level of accuracy.

> 1. Generate random X [-1 to 1]

As others have pointed out, this is already wrong unless you intend to reject some of the solutions later.

A potential issue with that is needing to adjust the distribution of either X or Y, since if X and Y remain uniform, then there will be a lot of points packed into the sides of the circle with higher density than the center column.