# Creating random points in a circle

392 views (last 30 days)
Stephen Johnson on 20 Apr 2013
Commented: Image Analyst on 5 Aug 2021
I am new to MatLab but I am trying to write code for a problem and a couple of things are sticking me up. I am trying to create X number of random points within a circle. The purpose being to model a camp fire and map temperature from the center of the circle at the hottest to the outside. And I am lost. And I can't find an example anywhere I might be able to pull apart and work with. And help would be very much appreciated.
Thanks!

Roger Stafford on 19 May 2013
Let the circle be of radius R and center at (x0,y0). Let n be the desired number of points within.
t = 2*pi*rand(n,1);
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
plot(x,y)
Jan on 21 Mar 2018
rand() replies values from the open interval (0,1), such that t=0 or t=2*pi will never occur. The difference to the correct distribution over [0,1) or (0,1] is tiny, but it is a pity, that Matlab's RNG does not offer half-closed or closed intervals also.

Image Analyst on 20 Apr 2013
Here, see my demo:
% M-file to place multiple points inside a big circle.
% Clean up
close all;
clc;
fontSize = 15;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Initialize some parameters.
numberOfPoints = 25; % Number of small circles
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
% Initialize an image to hold one single big circle.
bigCircleImage = zeros(bigImageHeight, bigImageWidth, 'uint8');
[x, y] = meshgrid(1:bigImageWidth, 1:bigImageHeight);
bigCircleImage((x - bigImageWidth/2).^2 + (y - bigImageHeight/2).^2 <= bigCircleRadius.^2) = 1;
clear('x', 'y'); % Release these variables, they're not needed anymore.
% Display it in the upper left plot.
subplot(2,2, 1);
imshow(bigCircleImage, []);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Initialize an output image to hold many small overlapping circles.
pointsImage = zeros(bigImageHeight, bigImageWidth, 'uint8');
% Get linear indexes of 500 randomly located in the rectangle.
numberOfPointsToPlace = 5000;
linearIndexes = randi(numel(pointsImage), numberOfPointsToPlace, 1);
% Set those points in the image
pointsImage(linearIndexes) = 255;
% Get locations in terms of row and columns:
[rows, columns] = ind2sub(size(pointsImage), linearIndexes);
% Display it in the lower left plot.
subplot(2,2, 2);
imshow(pointsImage);
title('Many Points', 'FontSize', fontSize);
% Multiply the big circle mask by the points image to clip
% those points that lie outside the big circle.
% Display it in the lower right plot.
subplot(2,2, 3);
title('Many Points Masked by Big Circle', 'FontSize', fontSize);
Image Analyst on 5 Aug 2021
Try this:
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
r = 2;
fprintf('Done running %s.m\n', mfilename);
missrate = 0;
% Make 360 points around the circle.
numCirclePoints = 360;
theta = linspace(0, 2 * pi, numCirclePoints);
% Get radius, ignoring the one passed in.
%r=20;
r=input(q);
xcenter=0;
ycenter=0;
radiusList = [r, r + r/20, r - r/20];
xcord = xcenter + r * cos(theta);
ycord = ycenter + r * sin(theta);
plot(xcord, ycord, 'b-', 'LineWidth', 2)
hold on
plot(xchigh,ychigh, 'm-', 'LineWidth', 2)
plot(xclow,yclow, 'g-', 'LineWidth', 2)
grid on;
numRandomPoints = 300;
x0 = 0; % Center of the circle in the x direction.
y0 = 0; % Center of the circle in the y direction.
% Create a noisy set of points
rng default
t = 2 * pi * rand(numRandomPoints,1);
g = 0.9 * r + 0.2 * r * rand(numRandomPoints,1); %generate random points in boundary
x = x0 + g.*cos(t);
y = y0 + g.*sin(t);
% Now display our random set of points in a figure with circle
colors = ['r', 'g', 'b', 'm']
for k = 1 : length(radiusList) - 1
thisx = x(logicalIndexes);
thisy = y(logicalIndexes);
thisColor = colors(k);
plot(thisx, thisy, '.', 'Color', thisColor, 'MarkerSize', 20)
end
hold off
axis equal
grid on;
end

Irfan on 31 Jul 2013
Hi, Roger, Why did you use sqrt in r = R*sqrt(rand(n,1)); it should be without sqrt, is'nt ?
Roger Stafford on 31 Jul 2013
That is untrue! Without the square root operation, the distribution of points within the circle will not be uniformly distributed throughout its interior. Just try it out with a large value of n, say, n = 1000 and see.
Think of it this way, Irfan. The area of a circle with the same center and with half the given radius, R, is one-fourth that of the larger circle. Therefore the probability that we get an r value less than or equal to R/2 should be one-fourth. That is, a 'rand' value of 1/4 should give rise to a value r = R/2. This is accomplished by taking the square root of the 'rand' value. A similar reasoning applies to other fractional circles.