MATLAB: Generate N random uniformly distributed points in a specific area

distributedrandom

I want to generate N random uniformly distributed points in the area between a circle of radius 1 and a square of side 2, both centered at origin like in the picture above.
How can I do that?

Best Answer

Here's a simple way (edited)
Randomly distributed:
N=100; %number of points
n=1; %Iterator
x_range=[-1 1]; %Range of width
mid_point=[mean(x_range),mean(x_range)]; %Center of box
radius=1; %Radius of circle
point_arr=zeros(N,2); %This will hold the point
while n<=N
temp_xy = (x_range(2)-x_range(1)).*rand(1,2) + x_range(1); %Generate 2 random numbers x and y
d = pdist([temp_xy;mid_point],'euclidean'); %Find distance between the point generated and the middle
if d>radius %If the distance is smaller than the radius, discard it, else accept it
point_arr(n,:)=temp_xy;
n=n+1;
end
end
scatter (point_arr(:,1),point_arr(:,2))