Choose n points randomly from a circle, how to calculate the probability that all the points are in one semicircle?
3 次查看(过去 30 天)
显示 更早的评论
I want to create a graphic monte carlo simulation that solves this problem, The mathematical answer is : n/2^(n-1)
maybe someone have the algorithm code for that question ?
2 个评论
Guillaume
2020-1-10
It's not entirely clear what you need help with. The problem itself is simple, so we don't know what's blocking you. You'd just run a monte-carlo simulation which is easy enough to implement.
The one difficultly I can see is the generation of points in a circular uniform distribution. You have to be careful not to use the naive approach of just picking uniform random radii and angles. However, less than 5 minutes of searching on the internet would give you simple and reliable algorithms for that, if it's not already built in matlab.
回答(1 个)
KSSV
2020-1-10
% Generate circle
r = 1. ; % radius
th = linspace(0,2*pi) ;
x = r*cos(th) ;
y = r*sin(th) ;
% Geenrate random points inside the circle
N = 5000 ;
a = -1 ; b = 1 ;
xr = (b-a).*rand(N,1) + a;
yr = (b-a).*rand(N,1) + a;
% pick points lying inside the circle
idx1 = inpolygon(xr,yr,x,y) ;
xr = xr(idx1) ;
yr = yr(idx1) ;
% random points inside circle
NP1 = nnz(idx1) ;
% points lying in semicircle
idx2 = inpolygon(xr,yr,x(1:50),y(1:50)) ;
NP2 = nnz(idx2) ;
iwant = NP2/NP1 ; % probability that points lie in semicircle
plot(x,y)
hold on
plot(xr,yr,'.r')
plot(xr(idx2),yr(idx2),'ob')
3 个评论
KSSV
2020-1-10
编辑:KSSV
2020-1-10
Yes you are right..I made the problem complex instead of using the inequality.
@elroi berkovits please noe that, you need not to use inpolygon. We have given you a idea, you should try the one you want.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!