84 views (last 30 days)
Show older comments
Shweta Rajoria on 9 Oct 2017
-
-
Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle
Answered: Moreno, M. on 20 Mar 2022
Accepted Answer: Torsten
Open in MATLAB Online
I used "rand" function to generate uniform random points within the circle, but points generated by this code are not uniformly distributed. The code used by me is given below.So, is there any way to do so?? If any body know the answer please help me. Any suggestions will be appreciated. Thanks in advance.
Code:
% Create a random set of coordinates in a circle.
% First define parameters that define the number of points and the circle.
n = 400;
R = 20;
x0 = 0; % Center of the circle in the x direction.
y0 = 0; % Center of the circle in the y direction.
% Now create the set of points.
t = 2*pi*rand(n,1);
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
% Now display our random set of points in a figure.
plot(x,y, 'o', 'MarkerSize', 5)
axis square;
grid on;
% Enlarge figure to full screen.
%set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
fontSize = 30;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Random Locations Within a Circle', 'FontSize', fontSize);
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Walter Roberson on 9 Oct 2017
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#comment_491207
That code should distribute uniformly random by area.
Remember, "uniform random" does not mean "does not look like it has clusters or obvious open space". Humans see open areas and tend to say "Oh, it isn't uniformly randomly distributed" when it is.
It is like flipping a coin a million times in a row, seeing that at some point there was 15 Tails in a row, and saying "Oh, that's a huge gap, that isn't uniformly randomly distributed!" (indeed, you would expect to see about 17 to 21 Tails in a row if the coin was fair.)
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Torsten on 9 Oct 2017
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Shweta Rajoria on 13 Oct 2017
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#comment_492662
Thank you sir.. it solves my problem.
Sign in to comment.
More Answers (3)
abbas sabbagh on 3 Mar 2022
Edited: abbas sabbagh on 3 Mar 2022
Open in MATLAB Online
Hi, please check this code. It is really uniformly distributed!
clear;
clc;
clf;
Npoint=10000;
R=2;
for i=1:(5/pi)*Npoint, % generating more than points needed (greedy coeff is 4/pi)!
x=unifrnd(-R,R);
y=unifrnd(-R,R);
if x^2 + y^2 <=R^2,
XX(i,:)=[x,y];
end
end
XX( all(~XX,2), : ) = []; % deleting zeros rows
XX=XX(1:Npoint,:); % choosing the first valid Npoint generated
scatter(XX(:,1),XX(:,2),'.');
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Nguyen Duc Du on 26 Jan 2019
Thank you so much !!
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Moreno, M. on 20 Mar 2022
https://uk.mathworks.com/matlabcentral/fileexchange/108374-uniformly-distributed-points
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and ArraysCreating and Concatenating Matrices
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Tags
- wireless communication
- random number generation
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office