How to initialize particles randomly in a circular structure?
显示 更早的评论
I need to randomly initialize 256 particles inside a 2D circle.
something like this:

2 个评论
Ravi Kumar
2021-12-28
John D'Errico
2021-12-28
The problem is, that is not random. It is quasi-uniform. But not remotely random.
采纳的回答
更多回答(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 个评论
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
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

