Generating a unit 3 vector in Python (Uniform Spherical Projection)

Andrew Bolster

Senior R&D Manager (Data Science) at Black Duck Software and Treasurer @ Bsides Belfast and NI OpenGovernment Network

Quick one more as a reminder to me than anything else.

As part of my PhD work I’m building different behaviours for virtual submarines. I’ll be explaining some parts of my work in a separate post, but basically, I needed to random walk. Random walk in 2 dimensions is easy; pick two random numbers, go that way. Unfortunately doesn’t work that way on a spherical surface

So to make things easier, I stole this StackOverflow answer from dmckee and tidied it up a bit for my purposes. (Assuming everyone else is like me and does import numpy as np)

def random_three_vector():
    """
    Generates a random 3D unit vector (direction) with a uniform spherical distribution
    Algo from http://stackoverflow.com/questions/5408276/python-uniform-spherical-distribution
    :return:
    """
    phi = np.random.uniform(0,np.pi*2)
    costheta = np.random.uniform(-1,1)

    theta = np.arccos( costheta )
    x = np.sin( theta) * np.cos( phi )
    y = np.sin( theta) * np.sin( phi )
    z = np.cos( theta )
    return (x,y,z)

# Example IPython code to test the uniformity of the distribution
from pylab import scatter
threetups = []
for _ in xrange(1000):
    threetups.append(random_three_vector())
zipped = zip(*threetups)
scatter(*zipped[0:2])
blog comments powered by Disqus