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

Alex Van de Kleut :

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.

user2357112 supports Monica :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=297868&siteId=1