How to generate say 100 points randomly in a square or rectangle?

161 views (last 30 days)
I want to generate 100 points in a square , which side length is 5 m.
  1 Comment
SUSHMA MB
SUSHMA MB on 3 Aug 2016
How to generate say 10 out of 100 random points on a straight line joining two points? I have two points let, (x1,y1) and (x2,y2). These two points are joined by a straight line. Now i want to generate, 10 points out of 100 random points on the straight line. Let the boundary be a square, with side length 50m

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 18 Sep 2013
Edited: Azzi Abdelmalek on 18 Sep 2013
x=rand(1,100)*5
y=rand(1,100)*5
scatter(x,y)
or
a=rand(2,100)*5
scatter(a(1,:),a(2,:))
  5 Comments
Tamoor Shafique
Tamoor Shafique on 5 Sep 2020
How can I generate 100 random points in randomly in one of the sphere/cube/rectangulare 3D space?
Akhil Govindraj
Akhil Govindraj on 9 Mar 2022
That's easy Tamoor Shafique, all you've gotta do is extend this code to the z axis as well and use scatter3() instead of scatter():
x=rand(1,100)*5;
y=rand(1,100)*5;
z=rand(1,100)*5;
scatter3(x,y,z)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 18 Sep 2013
Here's an alternate interpretation, if you want 100 locations in a matrix set to some value, such as 1.
% Randomly place a value of 1 at 100 locations.
m=10; % Whatever
% Make a "canvass" of all zeroes.
theArray = zeros(5*m);
% Get 100 linear indices randomly located
linearIndices = randperm(numel(theArray), 100);
% Make those 100 locations have a value of 1:
theArray(linearIndices) = 1;
  3 Comments
Tamoor Shafique
Tamoor Shafique on 5 Sep 2020
How can I generate 100 random points in randomly in one of the sphere/cube/rectangulare 3D space?

Sign in to comment.

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!