How to distribute random number of users within the different circles

3 views (last 30 days)
khalid khan
khalid khan on 6 May 2022
Commented: khalid khan on 10 May 2022
Hello Matlab Community,
I generated the random number of points (200 users) within 7 different circles (different radius) in square box of 1000 by 1000 meters as shown in figure. The number of points (users) in each circle are random but fix. I need to distribute the points (shuffle the users) among the mentioned cirlces for example one cirlce has 68 points (users) and other circle has 35 points (users) in the first iteration in the next iteration these users should be changed (shuffle from one cirlce to other circle) but the total number of points (200 users) among these circles in square box of 1000 by 1000 meters remain same.
Thank You!
  9 Comments
khalid khan
khalid khan on 10 May 2022
I am very thankful to every one for the positive comments and suggestions

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 10 May 2022
Edited: the cyclist on 10 May 2022
There are several ways to do this. Here is one.
I illustrated with 20 users, because with 200 users it is difficult to tell that the number of users per circle is changing.
FYI, with 200 users and 7 circles, if they are assigned randomly (with equal probability), you will get user counts per circle that are pretty close to each other.
% Set the random number generator seed, for reproducibility
rng default
% Number of iterations to run the algorithm
NITER = 5;
% Number of seconds to pause after each figure
NPAUSE = 1;
% Fixed circle parameters
NCIRCLES = 7;
x0 = [100 450 500 650 900 900 800]';
y0 = [700 200 600 500 400 100 850]';
% Number of users
NUSERS = 20; % <--- Change this to 200
for ni = 1:NITER
userCircle = randi(NCIRCLES,NUSERS,1);
usersPerCircle = histcounts(userCircle,1:NCIRCLES+1)';
circleRadius = sqrt(usersPerCircle);
figure
axis square
set(gca,"Box","on")
hold on
for nc = 1:NCIRCLES
tnc = 2*pi*rand(usersPerCircle(nc),1);
rnc = 10*circleRadius(nc)*rand(usersPerCircle(nc),1);
x = x0(nc) + rnc.*cos(tnc);
y = y0(nc) + rnc.*sin(tnc);
h = plot(x,y,".");
set(h,'MarkerSize',8)
end
pause(NPAUSE)
end

More Answers (1)

Riccardo Scorretti
Riccardo Scorretti on 6 May 2022
That's ok?
% Coordinates (x0,y0), radius (rad) and number of users (nbu) for each circle
x0 = [100 450 500 650 900 900 800];
y0 = [700 200 600 500 400 100 850];
rad = [ 30 40 50 30 35 35 30];
nbu = [ 25 35 68 15 44 10 15];
% Generate the users
nbu_tot = sum(nbu);
x = zeros(nbu_tot, 1); % it is better to pre-allocate matrix, when possible
y = zeros(nbu_tot, 1);
grp = zeros(nbu_tot, 1); % group to which each user belongs
base = 0;
for n = 1 : numel(nbu)
ind = base + (1:nbu(n)); % = index of the users
t = 2*pi*rand(nbu(n), 1);
r = rad(n)*sqrt(rand(nbu(n), 1));
x(ind) = x0(n) + r.*cos(t);
y(ind) = y0(n) + r.*sin(t);
grp(ind) = n;
base = base + nbu(n);
end
% Now, just shuffle the groups
grp = grp(randperm(nbu_tot));
% Plot the users
colors = 'rgbkmc'; % many colors ...
figure
for n = 1 : numel(nbu)
t = find(grp == n);
col = colors(1+mod(n,numel(colors)));
plot(x(t), y(t), [col '.'], 'MarkerSize', 5);
hold on
end
axis square ; grid on
It's a little bit different from your original code. The coordinates of all users are in vectors x and y. All users are grouped together; the group to which each user belong is stored in the vector grp.
  3 Comments
khalid khan
khalid khan on 9 May 2022
Thank you so much for the response
1) Yes, all the users should move at the same time.
2) yes smaller, for example if there are 25 users in circle and 15 users are move to another cirlce so the radius of this circle should reduce based on the users moves from it and radius of other cirlce should increase becasue users move to it.
3) The remaining users should be inside the circle not outside the circle.
4)No, the total numbers of cirlce should remain same 7 circles should be 7 if the users move to it or move from it the total circle should be 7 but the radius should increase or decrease of these circle based on the users move to it or move from it.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!