How to generate uniform random points with in a circle. (2024)

78 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

  • 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);

How to generate uniform random points with in a circle. (2)

1 Comment

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

  • Link

    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

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#answer_284868

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#answer_284868

1 Comment

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

  • Link

    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

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#answer_908790

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#answer_908790

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),'.');

How to generate uniform random points with in a circle. (7)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Nguyen Duc Du on 26 Jan 2019

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#answer_358125

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#answer_358125

Thank you so much !!

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Moreno, M. on 20 Mar 2022

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#answer_922504

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/360361-how-to-generate-uniform-random-points-with-in-a-circle#answer_922504

https://uk.mathworks.com/matlabcentral/fileexchange/108374-uniformly-distributed-points

0 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.


How to generate uniform random points with in a circle. (10)

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)

Europe

  • 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

How to generate uniform random points with in a circle. (2024)
Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6151

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.