32
7
The Challenge
Write a program or function that takes no input and outputs a vector of length \$1\$ in a theoretically uniform random direction.
This is equivalent to a random point on the sphere described by $$x^2+y^2+z^2=1$$
resulting in a distribution like such
Output
Three floats from a theoretically uniform random distribution for which the equation \$x^2+y^2+z^2=1\$ holds true to precision limits.
Challenge remarks
- The random distribution needs to be theoretically uniform. That is, if the pseudo-random number generator were to be replaced with a true RNG from the real numbers, it would result in a uniform random distribution of points on the sphere.
- Generating three random numbers from a uniform distribution and normalizing them is invalid: there will be a bias towards the corners of the three-dimensional space.
- Similarly, generating two random numbers from a uniform distribution and using them as spherical coordinates is invalid: there will be a bias towards the poles of the sphere.
- Proper uniformity can be achieved by algorithms including but not limited to:
- Generate three random numbers \$x\$, \$y\$ and \$z\$ from a normal (Gaussian) distribution around \$0\$ and normalize them.
- Generate three random numbers \$x\$, \$y\$ and \$z\$ from a uniform distribution in the range \$(-1,1)\$. Calculate the length of the vector by \$l=\sqrt{x^2+y^2+z^2}\$. Then, if \$l>1\$, reject the vector and generate a new set of numbers. Else, if \$l \leq 1\$, normalize the vector and return the result.
- Generate two random numbers \$i\$ and \$j\$ from a uniform distribution in the range \$(0,1)\$ and convert them to spherical coordinates like so:\begin{align}\theta &= 2 \times \pi \times i\\\\\phi &= \cos^{-1}(2\times j -1)\end{align}so that \$x\$, \$y\$ and \$z\$ can be calculated by \begin{align}x &= \cos(\theta) \times \sin(\phi)\\\\y &= \sin(\theta) \times \sin(\phi)\\\\z &= \cos(\phi)\end{align}
- Provide in your answer a brief description of the algorithm that you are using.
- Read more on sphere point picking on MathWorld.
Output examples
[ 0.72422852 -0.58643067 0.36275628]
[-0.79158628 -0.17595886 0.58517488]
[-0.16428481 -0.90804027 0.38532243]
[ 0.61238768 0.75123833 -0.24621596]
[-0.81111161 -0.46269121 0.35779156]
General remarks
- This is code-golf, so the answer using the fewest bytes in each language wins.
- Standard rules, I/O rules and loophole rules apply.
- Please include a Try it Online-link or equivalent to demonstrate your code working.
- Please motivate your answer with an explanation of your code.
Is it okay to pick 3 reals uniformly in [-1, 1], then reject them (and repeat) if the sum of their squares isn't 1? – Grimmy – 2019-09-09T13:06:30.837
6@Grimy I like that loophole. No, it is not allowed, because there is a theoretically zero chance of any output. – Jitse – 2019-09-09T13:25:15.180
Isn't @Grimy's suggestion similar to the second example implementation mentioned by you? That solution also has theoretically zero chance of producing any output – Saswat Padhi – 2019-09-09T20:59:37.240
2@SaswatPadhi No, that has a chance
pi/6 ≈ 0.5236
of producing an output. That's the area of the sphere inscribed in the unit-area cube – Luis Mendo – 2019-09-09T21:41:36.1501@LuisMendo I see, right. The probability is ~0.5 in that case, as you mentioned. For Grimy's proposal, it's ~0. – Saswat Padhi – 2019-09-09T21:47:41.593
Are we allowed to assume that uniform distributions over $[0,1]$, $[0, 1)$ and $(0, 1)$ are effectively the same? – ar4093 – 2019-09-10T08:51:09.320
@ar4093 Yes, that's fine. – Jitse – 2019-09-10T09:00:00.713