how to generate random point inside a sphere

24 ビュー (過去 30 日間)
Amrita Datta
Amrita Datta 2019 年 7 月 24 日
コメント済み: DGM 2024 年 2 月 23 日
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.

採用された回答

Torsten
Torsten 2019 年 7 月 24 日
編集済み: Torsten 2019 年 7 月 24 日
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
  1 件のコメント
Amrita Datta
Amrita Datta 2019 年 7 月 24 日
I want to like random points inside a sphere

サインインしてコメントする。

その他の回答 (2 件)

Najwa Yaminah
Najwa Yaminah 2020 年 1 月 17 日
編集済み: Ashish 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
John D'Errico 2020 年 1 月 28 日
Sigh. Of course, you posted this answer as Python code, when MATLAB code was needed.

サインインしてコメントする。


Patricia Sperry
Patricia Sperry 2019 年 9 月 21 日
編集済み: Ashish 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
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
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 ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by