Alex Van de Kleut
Alex Van de Kleut

Reputation: 150

Method for randomly generating points on surface of sphere don't look random

I'm trying to plot points randomly distributed on the surface of a sphere.

According to numerous sources (Method 10), the following should generate uniformly randomly distributed points:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt
N = 2000
r = 3
u = np.random.rand(N)
v = np.random.rand(N)
theta = 2*np.pi*u
phi = np.arccos(2*v-1)
x = r*np.cos(phi)*np.sin(theta)
y = r*np.sin(phi)*np.sin(theta)
z = r*np.cos(theta)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)

When I plot it, it doesn't look uniform. Some angles look like this:

this

whereas others look like this:

this

Even when I run the code from the website, plotting it looks wrong and non-uniform.

Upvotes: 2

Views: 161

Answers (1)

user2357112
user2357112

Reputation: 280778

The source you're looking at got phi and theta switched. Switch them back.

Upvotes: 3

Related Questions