How to initialize particles randomly in a circular structure?
Show older comments
I need to randomly initialize 256 particles inside a 2D circle.
something like this:

2 Comments
Ravi Kumar
on 28 Dec 2021
John D'Errico
on 28 Dec 2021
The problem is, that is not random. It is quasi-uniform. But not remotely random.
Accepted Answer
More Answers (1)
Depending on how you want the randomness to work, there would be different ways to do it. Here's a way that has the circle centered on the origin, and treats r (the distance of each point from the circle's center) and th (the angle that the vector from the circle's center to each point makes with the positive x-axis) as independent uniformly distributed random variables.
n = 256;
radius = 25;
r = radius*rand(1,n); % n uniformly distributed values between 0 and radius
th = 2*pi*rand(1,n); % n uniformly distributed values between 0 and 2*pi
x = r.*cos(th);
y = r.*sin(th);
figure();
plot(x,y,'*','MarkerSize',2);
2 Comments
To get a uniform distribution over the circle, you have to take
r = radius*sqrt(rand(1,n));
th = 2*pi*rand(1,n);
For an explanation, see
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
