Random point on a sphere

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

Random distribution of points on a sphere with radius 1.

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

Jitse

Posted 2019-09-09T07:14:11.060

Reputation: 3 566

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.150

1@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

Answers

36

Wolfram Language (Mathematica), 20 bytes

RandomPoint@Sphere[]

Try it online!

Does exactly what it says on the tin.

attinat

Posted 2019-09-09T07:14:11.060

Reputation: 3 495

24

R, 23 bytes

x=rnorm(3)
x/(x%*%x)^.5

Try it online!

Generates 3 realizations of the \$\mathcal N(0,1)\$ distribution and normalizes the resulting vector.

Plot of 1000 realizations:

enter image description here

Robin Ryder

Posted 2019-09-09T07:14:11.060

Reputation: 6 625

2Can you justify 3 axis distributed normally resulting in uniform distribution over the sphere ? (I don't see it) – Jeffrey supports Monica – 2019-09-09T20:17:11.577

4@Jeffrey It's pretty well-known in probability/statistics; but the proof for 2D (which extends neatly to 3 dimensions) is approximately: let $X,Y\sim N(0,1)$ and independent. Then $f_X(x)=Ke^{-\frac{1}{2}x^2}$ and $f_Y(y)=Ke^{-\frac{1}{2}y^2}$, so by independence $f_{XY}(x,y)=K^2e^{-\frac{1}{2}(x^2+y^2)}=f_Z(z)=K^2e^{-\frac{1}{2}\lVert z\rVert^2}$ where $z=(x,y)$, so it's clear that the distribution of $z$ depends solely on the magnitude of $z$, and thus the direction is uniformly distributed. – Giuseppe – 2019-09-09T21:07:12.833

1So, the normal distribution gives us uniformly distributed points around the circle, and dividing by the magnitude ensures the points lie on the circle – Giuseppe – 2019-09-09T21:18:14.203

23

x86-64 Machine Code - 63 62 55 49 bytes

6A 4F                push        4Fh  
68 00 00 80 3F       push        3F800000h  
C4 E2 79 18 4C 24 05 vbroadcastss xmm1,dword ptr [rsp+5]  
rand:
0F C7 F0             rdrand      eax  
73 FB                jnc         rand  
66 0F 6E C0          movd        xmm0,eax  
greaterThanOne:
66 0F 38 DC C0       aesenc      xmm0,xmm0  
0F 5B C0             cvtdq2ps    xmm0,xmm0  
0F 5E C1             divps       xmm0,xmm1  
C4 E3 79 40 D0 7F    vdpps       xmm2,xmm0,xmm0,7Fh  
0F 2F 14 24          comiss      xmm2,dword ptr [rsp]  
75 E9                jne         greaterThanOne
58                   pop         rax  
58                   pop         rax  
C3                   ret  

Uses the second algorithm, modified. Returns vector of [x, y, z, 0] in xmm0.

Explanation:

push 4Fh
push 3f800000h

Pushes the value for 1 and 2^31 as a float to the stack. The data overlap due to the sign extension, saving a few bytes.

vbroadcastss xmm1,dword ptr [rsp+5] Loads the value for 2^31 into 4 positions of xmm1.

rdrand      eax  
jnc         rand  
movd        xmm0,eax

Generates random 32-bit integer and loads it to bottom of xmm0.

aesenc      xmm0,xmm0  
cvtdq2ps    xmm0,xmm0  
divps       xmm0,xmm1 

Generates a random 32 bit integer, convert it to float (signed) and divide by 2^31 to get numbers between -1 and 1.

vdpps xmm2,xmm0,xmm0,7Fh adds the squares of the lower 3 floats using a dot product by itself, masking out the top float. This gives the length

comiss      xmm2,dword ptr [rsp]  
jne          rand+9h (07FF7A1DE1C9Eh)

Compares the length squared with 1 and rejects the values if it is not equal to 1. If the length squared is one, then the length is also one. That means the vector is already normalised and saves a square root and divide.

pop         rax  
pop         rax 

Restore the stack.

ret returns value in xmm0

Try it Online.

me'

Posted 2019-09-09T07:14:11.060

Reputation: 451

7+1 Using aesenc to produce 128 "random" bits is just beautiful. – DocMax – 2019-09-10T05:39:13.957

13

Python 2, 86 bytes

from random import*;R=random
z=R()*2-1
a=(1-z*z)**.5*1j**(4*R())
print a.real,a.imag,z

Try it online!

Generates the z-coordinate uniformly from -1 to 1. Then the x and y coordinates are sampled uniformly on a circle of radius (1-z*z)**.5.

It might not be obvious that the spherical distribution is in factor uniform over the z coordinate (and so over every coordinate). This is something special for dimension 3. See this proof that the surface area of a horizontal slice of a sphere is proportional to its height. Although slices near the equator have a bigger radius, slices near the pole are titled inward more, and it turns out these two effects exactly cancel.

To generate a random angle on this circle, we raise the imaginary unit 1j to a uniformly random power between 0 and 4, which saves us from needing trig functions, pi, or e, any of which would need an import. We then extract the real imaginary part. If we can output a complex number for two of the coordinates, the last line could just be print a,z.


86 bytes

from random import*
a,b,c=map(gauss,[0]*3,[1]*3)
R=(a*a+b*b+c*c)**.5
print a/R,b/R,c/R

Try it online!

Generates three normals and scales the result.


Python 2 with numpy, 57 bytes

from numpy import*
a=random.randn(3)
print a/sum(a*a)**.5

Try it online!

sum(a*a)**.5 is shorter than linalg.norm(a). We could also do dot(a,a) for the same length as sum(a*a). In Python 3, this can be shortened to a@a using the new operator @.

xnor

Posted 2019-09-09T07:14:11.060

Reputation: 115 687

1I like your first approach. I'm having trouble understanding how a bias towards the equator is avoided if z, from a uniform distribution, is left unmodified. – Jitse – 2019-09-09T08:12:04.113

2

@Jitse The spherical distribution is in factor uniform over each coordinate. This is something special for dimension 3. See for instance this proof that the surface area of a slice of a sphere is proportional to its height. Regarding the intuition that this is biased to the equator, note that while slices near the equator have a bigger radius, ones near the pole are titled inward more which gives more area, and it turns out these two effects exactly cancel.

– xnor – 2019-09-09T08:14:08.373

Very nice! Thanks for the clarification and the reference. – Jitse – 2019-09-09T08:23:04.813

@Jitse Thanks, I added it to the body. I realized though that I was only sampling positive z though, and fixed that for a few bytes. – xnor – 2019-09-09T08:26:13.283

1@Jitse Indeed, the surface area of a sphere equals the lateral surface area of the enclosing cylinder! – Neil – 2019-09-09T08:50:36.677

@Jitse Substituting $\phi$ into $x,y,z$ in the third provided algorithm results in the same approach. – Joel – 2019-09-09T13:07:02.830

@Joel You're absolutely right, not sure how I missed that. – Jitse – 2019-09-09T13:27:50.780

13

Octave, 40 33 22 bytes

We sample form a 3d standard normal distribution and normalize the vector:

(x=randn(1,3))/norm(x)

Try it online!

flawr

Posted 2019-09-09T07:14:11.060

Reputation: 40 560

For Octave only (i.e. not MATLAB), you can save a byte with this

– Tom Carpenter – 2019-09-10T08:40:42.497

1@TomCarpenter Thanks! In this case as it is just one expression we can even omit the disp:) – flawr – 2019-09-10T08:42:21.880

11

Unity C#, 34 bytes

f=>UnityEngine.Random.onUnitSphere

Unity has a builtin for unit sphere random values, so I thought I'd post it.

Draco18s no longer trusts SE

Posted 2019-09-09T07:14:11.060

Reputation: 3 053

Good use of a built in +1, You could just submit a function to be a bit shorter f=>Random.onUnitSphere – LiefdeWen – 2019-09-10T10:51:29.413

@LiefdeWen I knew about lambdas, I just wasn't sure if that was enough (in terms of validity on Code Golf) because it isn't declaring f's Type; using var only works inside a method and System.Func<Vector3> was longer. – Draco18s no longer trusts SE – 2019-09-10T13:21:07.130

1In codegolf returning a function is perfectly fine, and you don't have to count the declaration either meaning you can do sneaky things with dynamic parameters. You also don't count the last semi-colon. You do however count all using statements you add. so your byte count needs to include the using. But f=>Random.onUnitSphere is a perfectly valid submission – LiefdeWen – 2019-09-10T14:37:57.207

@LiefdeWen Yeah, I just wasn't sure how the declaration was handled and wasn't really feeling up to "searching meta." – Draco18s no longer trusts SE – 2019-09-10T15:35:45.693

f=>UnityEngine.Random.onUnitSphere saves you the using – Orace – 2019-09-11T10:17:02.270

@Orace Doh. I forgot about doing that. Originally it was needed (because of the Vector3; 2 fully qualifieds or 1 using). – Draco18s no longer trusts SE – 2019-09-11T13:19:42.157

6

Ruby, 34 50 49 bytes

->{[z=rand*2-1]+((1-z*z)**0.5*1i**(rand*4)).rect}

Try it online!

Returns an array of 3 numbers [z,y,x].

x and y are generated by raising i (square root of -1) to a random power between 0 and 4. This complex number needs to be scaled appropriately according to the z value in accordance with Pythagoras theorem: (x**2 + y**2) + z**2 = 1.

The z coordinate (which is generated first) is simply a uniformly distributed number between -1 and 1. Though not immediately obvious, dA/dz for a slice through a sphere is constant (and equal to the perimeter of a circle of the same radius as the whole sphere.) .

This was apparently discovered by Archimedes who described it in a very non-calculus-like way, and it is known as Archimedes Hat-Box theorem. See https://brilliant.org/wiki/surface-area-sphere/

Another reference from comments on xnor's answer. A surprisingly short URL, describing a surprisingly simple formula: http://mathworld.wolfram.com/Zone.html

Level River St

Posted 2019-09-09T07:14:11.060

Reputation: 22 049

@Jitse I forgot to scale back the x and y at high values of z. Effectively the points defined a cylinder. It's fixed now but it cost a lot of bytes! I could save a few if the the output can be expressed with a complex number [z, x+yi] I'll leave it as it is unless you say that's OK. – Level River St – 2019-09-09T09:58:12.373

Looks good! I really like this approach. For consistency, the required output is three floats, so I suggest leaving it like this. – Jitse – 2019-09-09T10:09:10.823

Why not use z*z instead of z**2? – Value Ink – 2019-09-09T21:06:36.213

@ValueInk yeah thanks I realised I'd missed that z*z. I've edited it in now. The other thing I could do is replace rand*4 with something like z*99 or x*9E9 (effectively limiting the possible values to a very fine spiral on the sphere) but I think that reduces the quality of the random. – Level River St – 2019-09-09T22:25:06.100

6

MATL, 10 bytes

1&3Xrt2&|/

Try it online!

Explanation

This uses the first approach described in the challenge.

1&3Xr  % Generate a 1×3 vector of i.i.d standard Gaussian variables
t      % Duplicate
2&|    % Compute the 2-norm
/      % Divide, element-wise. Implicitly display

Luis Mendo

Posted 2019-09-09T07:14:11.060

Reputation: 87 464

5

TI-BASIC, 15 bytes *

:randNorm(0,1,3
:Ans/√(sum(Ans²

Using the algorithm "generate 3 normally distributed values and normalize that vector".

Ending a program with an expression automatically prints the result on the Homescreen after the program terminates, so the result is actually shown, not just generated and blackholed.

*: randNorm( is a two-byte token, the rest are one-byte tokens. I've counted the initial (unavoidable) :, without that it would be 14 bytes. Saved as a program with a one-letter name, it takes 24 bytes of memory, which includes the 9 bytes of file-system overhead.

harold

Posted 2019-09-09T07:14:11.060

Reputation: 1 199

4

05AB1E, 23 22 bytes

[тε5°x<Ýs/<Ω}DnOtDî#}/

Implements the 2nd algorithm.

Try it online or get a few more random outputs.

Explanation:

NOTE: 05AB1E doesn't have a builtin to get a random decimal value in the range \$[0,1)\$. Instead, I create a list in increments of \$0.00001\$, and pick random values from that list. This increment could be changed to \$0.000000001\$ by changing the 5 to 9 in the code (although it would become rather slow..).

[            # Start an infinite loop:
 тε          #  Push 100, and map (basically, create a list with 3 values):
   5°        #   Push 100,000 (10**5)
     x       #   Double it to 200,000 (without popping)
      <      #   Decrease it by 1 to 199,999
       Ý     #   Create a list in the range [0, 199,999]
        s/   #   Swap to get 100,000 again, and divide each value in the list by this
          <  #   And then decrease by 1 to change the range [0,2) to [-1,1)
           Ω #   And pop and push a random value from this list
  }          #  After the map, we have our three random values
   D         #   Duplicate this list
    n        #   Square each inner value
     O       #   Take the sum of these squares
      t      #   Take the square-root of that
       D     #   Duplicate that as well
        î    #   Ceil it, and if it's now exactly 1:
         #   #    Stop the infinite loop
}/           # After the infinite loop: normalize by dividing
             # (after which the result is output implicitly)

Kevin Cruijssen

Posted 2019-09-09T07:14:11.060

Reputation: 67 575

1Using $l<1$ is equally as valid as $l\leq1$. The only criterion for $l\leq x$ is that $0< x\leq 1$. You may just as well accept vectors with $l<0.5$ if it would save any bytes. Any value smaller than or equal to $1$ removes the bias. – Jitse – 2019-09-09T09:28:29.440

@Jitse Ok, implemented the normalization in both my Java and 05AB1E answers. I hope everything is correct now. – Kevin Cruijssen – 2019-09-09T09:53:22.663

@Jitse Actually saved a byte by checking $v\leq1$ as $\lceil v\rceil==1$, instead of $v\lt1$. But thanks for the clarification that only $0\lt x\leq1$ is a requirement, and there isn't a strict $\leq$ requirement on $l$, as long as it's $\leq1$. – Kevin Cruijssen – 2019-09-09T10:11:39.557

3

JavaScript (ES7),  77 76  75 bytes

Implements the 3rd algorithm, using \$\sin(\phi)=\sin(\cos^{-1}(z))=\sqrt{1-z^2}\$.

with(Math)f=_=>[z=2*(r=random)()-1,cos(t=2*PI*r(q=(1-z*z)**.5))*q,sin(t)*q]

Try it online!

Commented

with(Math)                       // use Math
f = _ =>                         //
  [ z = 2 * (r = random)() - 1,  // z = 2 * j - 1
    cos(                         //
      t =                        // θ =
        2 * PI *                 //   2 * π * i
        r(q = (1 - z * z) ** .5) // q = sin(ɸ) = sin(arccos(z)) = √(1 - z²)
                                 // NB: it is safe to compute q here because
                                 //     Math.random ignores its parameter(s)
    ) * q,                       // x = cos(θ) * sin(ɸ)
    sin(t) * q                   // y = sin(θ) * sin(ɸ)
  ]                              //

JavaScript (ES6), 79 bytes

Implements the 2nd algorithm.

f=_=>(n=Math.hypot(...v=[0,0,0].map(_=>Math.random()*2-1)))>1?f():v.map(x=>x/n)

Try it online!

Commented

f = _ =>                         // f is a recursive function taking no parameter
  ( n = Math.hypot(...           // n is the Euclidean norm of
      v =                        // the vector v consisting of:
        [0, 0, 0].map(_ =>       //
          Math.random() * 2 - 1  //   3 uniform random values in [-1, 1]
        )                        //
  )) > 1 ?                       // if n is greater than 1:
    f()                          //   try again until it's not
  :                              // else:
    v.map(x => x / n)            //   return the normalized vector

Arnauld

Posted 2019-09-09T07:14:11.060

Reputation: 111 334

3

Processing 26 bytes

Full program

print(PVector.random3D());

This is the implementation https://github.com/processing/processing/blob/master/core/src/processing/core/PVector.java

  static public PVector random3D(PVector target, PApplet parent) {
    float angle;
    float vz;
    if (parent == null) {
      angle = (float) (Math.random()*Math.PI*2);
      vz    = (float) (Math.random()*2-1);
    } else {
      angle = parent.random(PConstants.TWO_PI);
      vz    = parent.random(-1,1);
    }
    float vx = (float) (Math.sqrt(1-vz*vz)*Math.cos(angle));
    float vy = (float) (Math.sqrt(1-vz*vz)*Math.sin(angle));
    if (target == null) {
      target = new PVector(vx, vy, vz);
      //target.normalize(); // Should be unnecessary
    } else {
      target.set(vx,vy,vz);
    }
    return target;
  }

PrincePolka

Posted 2019-09-09T07:14:11.060

Reputation: 653

2You might want to make it clearer that the implementation is not part of your byte count. I missed it on first reading, then did a double-take. – Level River St – 2019-09-09T18:35:54.970

I like that the implementation uses essentially the same approach as me, though – Level River St – 2019-09-09T18:38:02.887

2

Python 2, 86 bytes

from random import*
x,y,z=map(gauss,[0]*3,[1]*3);l=(x*x+y*y+z*z)**.5
print x/l,y/l,z/l

Try it online!

Implements the first algorithm.


Python 2, 107 103 bytes

from random import*
l=2
while l>1:x,y,z=map(uniform,[-1]*3,[1]*3);l=(x*x+y*y+z*z)**.5
print x/l,y/l,z/l

Try it online!

Implements the second algorithm.

TFeld

Posted 2019-09-09T07:14:11.060

Reputation: 19 246

2@RobinRyder This implementation rejects vectors with an initial length >1, which is valid as specified in the challenge. – Jitse – 2019-09-09T07:50:26.517

@Jitse Right, sorry. I misread the code. – Robin Ryder – 2019-09-09T07:51:24.430

2

Java 8 (@Arnauld's modified 3rd algorithm), 131 126 119 111 109 bytes

v->{double k=2*M.random()-1,t=M.sqrt(1-k*k),r[]={k,M.cos(k=2*M.PI*M.random())*t,M.sin(k)*t};return r;}

Port of @Arnauld's JavaScript answer, so make sure to upvote him!
-2 bytes thanks to @OlivierGrégoire.

This is implemented as:

\$k = N\cap[-1,1)\$
\$t=\sqrt{1-k^2}\$
\$u=2\pi×(N\cap[0,1))\$
\$x,y,z = \{k, \cos(u)×t, \sin(u)×t\}\$

Try it online.

Previous 3rd algorithm implementation (131 126 119 bytes):

Math M;v->{double k=2*M.random()-1,t=2*M.PI*M.random();return k+","+M.cos(t)*M.sin(k=M.acos(k))+","+M.sin(t)*M.sin(k);}

Implemented as:

\$k = N\cap[-1,1)\$
\$t=2\pi×(N\cap[0,1))\$
\$x,y,z = \{k, \cos(t)×\sin(\arccos(k)), \sin(t)×\sin(\arccos(k))\}\$

Try it online.

Explanation:

Math M;                         // Math on class-level to use for static calls to save bytes
v->{                            // Method with empty unused parameter & double-array return
  double k=2*M.random()-1,      //  Get a random value in the range [-1,1)
         t=M.sqrt(1-k*k),       //  Calculate the square-root of 1-k^2
    r[]={                       //  Create the result-array, containing:
         k,                     //   X: the random value `k`
         M.cos(k=2*M.PI         //   Y: first change `k` to TAU (2*PI)
                     *M.random()//       multiplied by a random [0,1) value
                )               //      Take the cosine of that
                 *t,            //      and multiply it by `t`
         M.sin(k)               //   Z: Also take the sine of the new `k` (TAU * random)
                  *t};          //      And multiply it by `t` as well
  return r;}                    //  Return this array as result

Java 8 (2nd algorithm), 153 143 bytes

v->{double x=2,y=2,z=2,l;for(;(l=Math.sqrt(x*x+y*y+z*z))>1;y=m(),z=m())x=m();return x/l+","+y/l+","+z/l;};double m(){return Math.random()*2-1;}

Try it online.

2nd algorithm:

v->{                              // Method with empty unused parameter & String return-type
  double x=2,y=2,z=2,l;           //  Start results a,b,c all at 2
  for(;(l=Math.sqrt(x*x+y*y+z*z)) //  Loop as long as the hypotenuse of x,y,z
       >1;                        //  is larger than 1
    y=m(),z=m())x=m();            //   Calculate a new x, y, and z
  return x/l+","+y/l+","+z/l;}    //  And return the normalized x,y,z as result
double m(){                       // Separated method to reduce bytes, which will:
  return Math.random()*2-1;}      //  Return a random value in the range [-1,1)

Kevin Cruijssen

Posted 2019-09-09T07:14:11.060

Reputation: 67 575

Using sqrt(1-k*k) actually saves more bytes in Java than it does in JS. :) – Arnauld – 2019-09-09T11:11:05.033

@Arnauld Yep. Instead of 3x M.sin, 1x M.cos and 1x M.acos, your approach uses 2x M.sin and 1x M.sqrt, which is where the additional saved bytes mostly come from. :) – Kevin Cruijssen – 2019-09-09T11:18:11.203

108 bytes Uses a modified 2nd algorithm where I only allow values where s == 1 (instead of s<=1 and then normalizing). It sometimes gives an answer but mostly doesn't because of the timeout. Edit: Oops, I forgot to Math.sqrt the result – Olivier Grégoire – 2019-09-11T12:41:40.670

Actually, no, no need to sqrt because sqrt(1)==1. So I stand with my golf suggestion. – Olivier Grégoire – 2019-09-11T12:48:52.880

Aaah, it's been banned as a loophole -_-' My bad... – Olivier Grégoire – 2019-09-11T12:57:19.717

1109 bytes (you can use your string output instead of double[] as that doesn't change the byte-count.) – Olivier Grégoire – 2019-09-11T13:35:45.720

@OlivierGrégoire Thanks! Nice switch of variable values. :) – Kevin Cruijssen – 2019-09-12T07:26:05.287

2

Haskell, 125 123 119 118 bytes

import System.Random
f=mapM(\_->randomRIO(-1,1))"lol">>= \a->last$f:[pure$(/n)<$>a|n<-[sqrt.sum$map(^2)a::Double],n<1]

Try it online!

Does three uniforms randoms and rejection sampling.

Angs

Posted 2019-09-09T07:14:11.060

Reputation: 4 825

It looks like your randoms are from the distribution (0,1) instead of (-1,1), so that only 1/8 of the sphere is covered. – Jitse – 2019-09-09T08:21:12.177

@Jitse gotcha, thanks for noticing. – Angs – 2019-09-09T08:24:08.990

2

MathGolf, 21 19 18 bytes

{╘3Ƀ∞(ß_²Σ√_1>}▲/

Implementation of the 2nd algorithm.

Try it online or see a few more outputs at the same time.

Explanation:

{              }▲   # Do-while true by popping the value:
 ╘                  #  Discard everything on the stack to clean up previous iterations
  3É                #  Loop 3 times, executing the following three operations:
    ƒ               #   Push a random value in the range [0,1]
     ∞              #   Double it to make the range [0,2]
      (             #   Decrease it by 1 to make the range [-1,1]
       ß            #  Wrap these three values into a list
        _           #  Duplicate the list of random values
         ²          #  Square each value in the list
          Σ         #  Sum them
           √        #  And take the square-root of that
            _       #  Duplicate it as well
             1>     #  And check if it's larger than 1
                 /  # After the do-while, divide to normalize
                    # (after which the entire stack joined together is output implicitly,
                    #  which is why we need the `╘` to cleanup after every iteration)

Kevin Cruijssen

Posted 2019-09-09T07:14:11.060

Reputation: 67 575

2

JavaScript, 95 bytes

f=(a=[x,y,z]=[0,0,0].map(e=>Math.random()*2-1))=>(s=Math.sqrt(x*x+y*y+z*z))>1?f():a.map(e=>e/s)

You don't need not to input a.

Naruyoko

Posted 2019-09-09T07:14:11.060

Reputation: 459

Wow, I completely missed that. Fixed. – Naruyoko – 2019-09-09T23:12:45.077

2

Julia 1.0, 24 bytes

x=randn(3)
x/hypot(x...)

Try it online!

Draws a vector of 3 values, drawn from a normal distribution around 0 with standard deviation 1. Then just normalizes them.

user3263164

Posted 2019-09-09T07:14:11.060

Reputation: 381

randn(), from a couple of quick tests, doesn't seem to be bound to the required range. Also, this doesn't include a check for hypot() returning a value >1, which should be rejected. – Shaggy – 2019-09-10T21:02:53.297

3@Shaggy it would appear randn simulates from a standard normal distribution rather than a uniform(0,1) one, so this approach is identical to the R one. – Giuseppe – 2019-09-10T21:04:51.640

@Giuseppe Yes, exactly! – user3263164 – 2019-09-10T21:05:33.897

@Giuseppe, I think that I may not have a proper grasp on the maths behind this challenge but, if I'm understanding you correctly, you're saying that if any of the floats are outside the bounds of [-1,1) then dividing by them by the hypotenuse, which will be >1, offsets that? That leads me to wonder if the ternary in my solution is necessary ... – Shaggy – 2019-09-10T23:15:12.823

@Shaggy no, the normal/Gaussian distribution has some properties (specifically, rotational invariance) that the uniform doesn't have, see this comment, for example

– Giuseppe – 2019-09-10T23:19:48.760

1

Pyth, 24 bytes

W<1Ks^R2JmtO2.0 3;cR@K2J

Try it online!

Uses algorithm #2

W                         # while 
 <1                       #   1 < 
   Ks                     #       K := sum(
     ^R2                  #               map(lambda x:x**2,
        Jm      3         #                    J := map(                            , range(3))
          tO2.0           #                             lambda x: random(0, 2.0) - 1           )):
                 ;        #   pass
                   R   J  # [return] map(lambda x:            , J)
                  c @K2   #                        x / sqrt(K)

ar4093

Posted 2019-09-09T07:14:11.060

Reputation: 531

1

Japt, 20 bytes

Port of Arnauld's implementation of the 2nd algorithm.

MhV=3ÆMrJ1
>1?ß:V®/U

Test it

MhV=3ÆMrJ1
Mh             :Get the hypotenuse of
  V=           :  Assign to V
    3Æ         :  Map the range [0,3)
      Mr       :    Random float
        J1     :    In range [-1,1)
>1?ß:V®/U      :Assign result to U
>1?            :If U is greater than 1
   ß           :  Run the programme again
    :V®/U      :Else map V, dividing all elements by U

Shaggy

Posted 2019-09-09T07:14:11.060

Reputation: 24 623

1

OCaml, 110 99 95 bytes

(fun f a c s->let t,p=f 4.*.a 0.,a(f 2.-.1.)in[c t*.s p;s t*.s p;c p])Random.float acos cos sin

EDIT: Shaved off some bytes by inlining \$ i \$ and \$ j \$, replacing the first let ... in with a fun, and taking advantage of operator associativity to avoid some parens ().

Try it online


Original solution:

Random.(let a,c,s,i,j=acos,cos,sin,float 4.,float 2. in let t,p=i*.(a 0.),a (j-.1.) in[c t*.s p;s t*.s p;c p])

First I define:

$$ a = \arccos,\ \ c = \cos,\ \ s = \sin \\ i \sim \textsf{unif}(0,4),\ \ j \sim \textsf{unif}(0,2) $$

OCaml's Random.float function includes the bounds. Then,

$$ t = i \cdot a(0) = \frac{i\pi}{2},\ \ p = a (j-1) $$

This is very similar to the 3rd example implementation (with \$ \phi = p \$ and \$ \theta = t \$) \$ - \$ except that I pick \$ i \$ and \$ j \$ within larger intervals to avoid multiplication (with 2) later on.

Saswat Padhi

Posted 2019-09-09T07:14:11.060

Reputation: 111

1I'm not quite familiar with this language, but it looks like you use the random floats between 0 and 1 directly as spherical coordinates. This is incorrect, as shown in the challenge remarks 3 and 4, since you end up with a bias towards the poles of the sphere. You can correct this by applying the method shown in remark 4. – Jitse – 2019-09-10T07:27:27.123

Thank you! Totally missed that. Fixed the bug and updated my answer – Saswat Padhi – 2019-09-10T08:25:02.097

1Looks good! Very nice first answer! – Jitse – 2019-09-10T08:27:43.690

Thank you :) I was able to reduce it to under-100 bytes! – Saswat Padhi – 2019-09-10T08:38:12.420