I am trying to generate random, uniformly distributed points on a sphere. However, the code is creating points that seem to create a disk instead. I believe that the issue resides in the "phirand" definition. Is the math there not correct? I used the same code in Matlab and it worked in that.
Code:
import numpy as np
import pylab
from scipy.integrate import odeint
import matplotlib.pyplot as plt
#import random
import mpl_toolkits.mplot3d.axes3d as p3
import random as rand
particlecount = 10 ## of particles to generate energies for energy generation
binsize = 15 #Determines bin size for historgram of electron energies
RStart = 0.02
phi1 = 0
phi2 = 180
phi1rad = phi1*(np.pi/180)
phi2rad = phi2*(np.pi/180)
#Generate random positions for each particle between s1 and s2
ICPositions = np.array([])
for i in range(0,particlecount):
#In Spherical: Generates random position with boundaries of: S1<r<S2
thetarand = (2*np.pi)*rand.uniform(0,1) #Random # generation for component y between s1 and s2
phirand = np.arcsin((np.sin(phi2rad) - np.sin(phi1rad))*rand.uniform(0,1) + np.sin(phi1rad))
xrand = RStart*np.sin(phirand)*np.cos(thetarand)
yrand = RStart*np.sin(phirand)*np.sin(thetarand)
zrand = RStart*np.cos(phirand)
randArray = np.array([xrand,yrand,zrand])
randArray = np.array(randArray,dtype = float)
if ICPositions.size == 0:
ICPositions = np.array([randArray])
else:
ICPositions = np.append(ICPositions,[randArray],axis = 0)
print(ICPositions)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(ICPositions[:,0],ICPositions[:,1],ICPositions[:,2],c='r',marker='o')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.show()