Generate random polar angles

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?

 Risposta accettata

Roger Stafford
Roger Stafford il 29 Mag 2014
Modificato: Roger Stafford il 29 Mag 2014
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)

Più risposte (0)

Categorie

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by