how to generate random point inside a sphere
조회 수: 7 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
추가 답변 (2개)
Najwa Yaminah
2020년 1월 17일
편집: Staff 3
2020년 5월 13일
I have an assignment so I try the following code :
function getPoint() {
var d, x, y, z;
do {
x = Math.random() * 2.0 - 1.0;
y = Math.random() * 2.0 - 1.0;
z = Math.random() * 2.0 - 1.0;
d = x*x + y*y + z*z;
} while(d > 1.0);
return {x: x, y: y, z: z};
}
This works well as you can see

댓글 수: 1
John D'Errico
2020년 1월 28일
Sigh. Of course, you posted this answer as Python code, when MATLAB code was needed.
Patricia Sperry
2019년 9월 21일
편집: Staff 3
2020년 5월 13일
Floow this method for generating the random points on sphere. I have also used this method for doing my work for the project.
Do try this code:
TH = 2*pi*rand(1,1e4);
PH = asin(-1+2*rand(1,1e4));
[X,Y,Z] = sph2cart(TH,PH,1);
plot3(X,Y,Z,'.','markersize',1)
axis equal vis3d
댓글 수: 2
darova
2019년 9월 21일
I like how it looks like but all points are on sphere surface (not inside)
I think random radius should be added
DGM
2024년 2월 23일
Considering that I just deleted 8 other junk answers used for spam links, something tells me that these answers were also spam but have since had their links removed. That might explain why they're not relevant to the question.
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!