15 views (last 30 days)

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 ?

KSSV
on 10 Jan 2020

% 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')

Guillaume
on 10 Jan 2020

KSSV
on 10 Jan 2020

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.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/499722-choose-n-points-randomly-from-a-circle-how-to-calculate-the-probability-that-all-the-points-are-in#comment_784851

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/499722-choose-n-points-randomly-from-a-circle-how-to-calculate-the-probability-that-all-the-points-are-in#comment_784851

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/499722-choose-n-points-randomly-from-a-circle-how-to-calculate-the-probability-that-all-the-points-are-in#comment_784867

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/499722-choose-n-points-randomly-from-a-circle-how-to-calculate-the-probability-that-all-the-points-are-in#comment_784867

Sign in to comment.