Home Activity Blog Project Euler

I'm a Machine Learning engineer working for Klarna in Stockholm, Sweden. I work in the AI & Automation group, where we are tackling the problem of web automation at scale.

Prior to this, I completed my PhD in Astrophysics in 2018. I worked on a wide range of topics in astronomy, machine learning and statistics.

### Contact

Feel free to email me
jimbarrett27@gmail.com

Or find me via my various online presences below

## Random Samples from a Sphere

#### 26/07/2017 - Jim Barrett

During my work today, at some point I had to draw samples uniformly from a sphere, and made an observation I hadn't really appreciated before. The name of the game is to make sure that every infinitesimal shell has the same density of samples, on average.

$$\frac{4\pi r^2}{\left< N_{samp} \right>} = \mathrm{constant}$$

Or in other words, the volume gets more spread out, so we need more samples for higher r. The density is simply constant with respect to direction (angles). So, to get the correct distribution we simply need to sample from

$$\theta \sim \mathcal{U}(0,2\pi)$$

$$\phi \sim \mathcal{U}(0,\pi)$$

$$r \sim r^2$$

Or in other words, just sample from a power law distribution with index 2. Elementary observation? Maybe, but it makes it easier to code up!