MATLAB: How to generate random point inside a sphere

sphere

I am trying to generate random points inside a sphere given the radius and the center. If anyone has a sample code or can help me with this, thanks for the help.

Best Answer

R = 3; % radius of the sphere
Xc = 4;
Yc = -4;
Zc = pi; % (Xc,Yc,Zc) center of the sphere
rval = 2*rand(1)-1;
elevation = asin(rval);
azimuth = 2*pi*rand(1);
radius = R*(rand(1))^(1/3);
[x,y,z] = sph2cart(azimuth,elevation,radius);
x=x+Xc
y=y+Yc
z=z+Zc