MATLAB: How to generate random points located on the surface of a hemisphere with its center at (2, 1, 3) and a radius of 6

hemisphere

N=500;
r = 6;
center = [2,1,3];
plot3(center(1),center(2),center(3),'ro')
hold on
for i=1:N
x1 = rand;
x2 = rand;
Px = sqrt(x1*(2-x1))*cos(2*pi*x2)+ center(1);
Py = sqrt(x1*(2-x1))*sin(2*pi*x2)+ center(2);
Pz = 1-x1 + center(3);
plot3(Px, Py, Pz,'o'),hold on
end
p = [Px, Py, Pz];
dist = Ddist(center,p);
What am I missing here? I'm using the dist variable to be sure I'll get 6, but currently I get 1

Best Answer

N=5000;
r = 6;
center = [2,1,3];
P = zeros(N,3) ;
plot3(center(1),center(2),center(3),'ro')
hold on
for i=1:N
x1 = rand;
x2 = rand;
Px = sqrt(x1*(2-x1))* cos(2*pi*x2);
Py = sqrt(x1*(2-x1))* sin(2*pi*x2);
Pz = 1-x1;
Px = Px * r + center(1);
Py = Py * r + center(2);
Pz = Pz * r + center(3);
P(i,:) = [Px, Py, Pz];
plot3(Px, Py, Pz,'o'),hold on
end