How to create random users in a defined area of a circle

6 views (last 30 days)
Hello,
I am trying to create three random users, each in any of the drawn circles, with a base station at the center (0+0i). Attached is the code and the output.
I will be glad if someone can help me out.
clc;
close all;
clear all
xu = 0; yu = 0;
BS_loc = xu+1i*yu; %BaseStation location
R1 = 15; %diameter of cells
R2 = 10;
R3 = 5;
vectX1=[]; vectX2=[]; vectX3=[];
vectY1=[]; vectY2=[]; vectY3=[];
for theta = -pi:0.01:pi
x1=R1*cos(theta);
y1=R1*sin(theta);
x2=R2*cos(theta);
y2=R2*sin(theta);
x3=R3*cos(theta);
y3=R3*sin(theta);
vectX1=[vectX1 x1]; vectX2=[vectX2 x2]; vectX3=[vectX3 x3];
vectY1=[vectY1 y1]; vectY2=[vectY2 y2]; vectY3=[vectY3 y3];
end
plot(vectX1,vectY1,'r');
hold on
plot(vectX2,vectY2,'g');
hold on
plot(vectX3,vectY3,'b');
hold on
plot(xu,yu,'kx','MarkerSize',6, 'LineWidth',2);
grid on

Answers (1)

BhaTTa
BhaTTa on 18 Jul 2025
Hey @Eugene Fab, Here's how you can modify your code to place three random users within a specified circle (let's say R1 for now, then you can adapt it for different circles or zones). I have provided an updated script below:
clc;
close all;
clear all;
xu = 0; yu = 0;
BS_loc = xu + 1i * yu; % Base Station location
R1 = 15; % Radius of outermost cell
R2 = 10; % Radius of middle cell
R3 = 5; % Radius of innermost cell
% --- Plotting the circles (your existing code, slightly cleaner) ---
figure; % Create a new figure
hold on; % Keep plots on the same axes
theta_plot = linspace(0, 2*pi, 100); % Use linspace for smoother circles
plot(R1 * cos(theta_plot), R1 * sin(theta_plot), 'r', 'LineWidth', 1.5);
plot(R2 * cos(theta_plot), R2 * sin(theta_plot), 'g', 'LineWidth', 1.5);
plot(R3 * cos(theta_plot), R3 * sin(theta_plot), 'b', 'LineWidth', 1.5);
plot(xu, yu, 'kx', 'MarkerSize', 8, 'LineWidth', 2); % Base Station
grid on;
axis equal; % Ensure circles look circular
xlabel('X-coordinate');
ylabel('Y-coordinate');
title('Base Station and Random Users in Circular Cells');
% --- Generating Random Users ---
num_users = 3; % Number of users to generate
% Method 1: Randomly place users within the *entire* R1 circle
% Each user has an equal chance of being anywhere in R1
disp('Generating users in the outermost (R1) circle:');
Generating users in the outermost (R1) circle:
for i = 1:num_users
% Generate a random angle between 0 and 2*pi
random_theta = 2 * pi * rand;
% Generate a random radius between 0 and R1, but NOT linearly
% For uniform distribution in a circle, radius squared should be uniform
random_r = R1 * sqrt(rand);
% Convert polar to Cartesian coordinates
user_x = random_r * cos(random_theta);
user_y = random_r * sin(random_theta);
plot(user_x, user_y, 'mo', 'MarkerSize', 6, 'MarkerFaceColor', 'm'); % Plot user
text(user_x + 0.5, user_y + 0.5, ['User ' num2str(i)]); % Label user
fprintf('User %d: (%.2f, %.2f)\n', i, user_x, user_y);
end
User 1: (1.93, 11.23) User 2: (7.44, -3.01) User 3: (4.19, -3.12)
% You mentioned "each in any of the drawn circles". Let's show how to place
% them specifically within the *annulus* between R2 and R1.
% Method 2: Randomly place users in the annulus (R2 to R1)
fprintf('\nGenerating users in the annulus between R2 (green) and R1 (red):\n');
Generating users in the annulus between R2 (green) and R1 (red):
num_users_annulus = 2; % Example: 2 users in the annulus
for i = 1:num_users_annulus
% Generate a random angle between 0 and 2*pi
random_theta_annulus = 2 * pi * rand;
% Generate a random radius for the annulus: R_inner^2 + (R_outer^2 - R_inner^2) * rand
random_r_annulus = sqrt(R2^2 + (R1^2 - R2^2) * rand);
user_x_annulus = random_r_annulus * cos(random_theta_annulus);
user_y_annulus = random_r_annulus * sin(random_theta_annulus);
plot(user_x_annulus, user_y_annulus, 'cs', 'MarkerSize', 7, 'MarkerFaceColor', 'c'); % Plot user as square
text(user_x_annulus + 0.5, user_y_annulus + 0.5, ['Annulus User ' num2str(i)]); % Label user
fprintf('Annulus User %d: (%.2f, %.2f)\n', i, user_x_annulus, user_y_annulus);
end
Annulus User 1: (10.05, -5.13) Annulus User 2: (3.39, 14.01)
hold off;

Categories

Find more on Creating and Concatenating Matrices 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!