MATLAB: How to generate the random number inside the annulus

random number generator

Hi everyone,
I want to generate 50 particles inside the gap between two circles. The small circle has radius of 5 and the outer circle has radius of 5.12. I want to make sure the particles do not overlap.
Also, can we assign a seed number so the positions of particles will be the same every time i used the same number of seed instead based on clock of computer?
Please helps.
Thanks

Best Answer

% Set your seed here if desired
n = 50;
r1 = 5; r2 = 5.12;
r = sqrt(r1^2+(r2^2-r1^2)*rand(1,n)); % Using square root here ensures distribution uniformity by area
t = 2*pi*rand(1,n);
x = r.*cos(t);
y = r.*sin(t);
plot(x,y,'y.')
axis equal
Note: You worry me where you say "make sure the particles do not overlap". The yellow dots here are assumed to have zero width so they can't overlap.
Related Question