MATLAB: Generate random polar angles

MATLABpolar anglepolar angle distributionuniform distributionunit sphere

I want to generate random polar angles given a uniform distribution on the unit sphere. In other words, I want to generate random numbers from the probability density function p(theta) = sin(theta)/2 in the interval [0,pi]. How do I do this?

Best Answer

The requested distribution of theta is generated below. Consequently the following code will places yellow dots on the unit sphere surface in a statistically uniform manner.
n = 8192;
theta = acos(1-2*rand(n,1)); % <-- The requested distribution
phi = 2*pi*rand(n,1);
x = sin(theta).*cos(phi);
y = sin(theta).*sin(phi);
z = cos(theta);
plot(x,y,z,'y.')
(Corrected)
Related Question