Random points moving from boundary

Hello
I have square surface(300kmto300km) and I am picking random points(20points) in the surface. My task is my random points should not be close than 10 km to square borders. So how I can give command that if my random point less than 10 km close to square borders choose another point.
Thanks a lot!

 Accepted Answer

Rather than checking if a random point is outside your allowed range and retrying, you'd generate random points within your allowed range to start with:
allowedrange = [10 290];
numpoints = 20;
xy = rand([2, numpoints) * diff(allowedrange) + allowedrange(1) %generate numpoints pairs
See the documentation on how to generate random numbers within a range.

3 Comments

Thanks Guillaume for the response. It is very close what I want to do. Actually I gave my question a little simple way.Could you please help me to my question again if you can?
So I have box borders X(1.7,7.7) and Y(42.3,47.3). I should choose random 18 small square boxes in the box. The with or length(actually width=length as it is square) of the small boxes would be from 0.1-2.2. The small boxes must be in the big box.
Thank you for your time.
This should work:
sizerange = [0.1 2.2];
boxsize = rand(18, 1) * diff(sizerange) + sizerange(1);
topleft = [1.7 42.3]
bottomright = [7.7 47.3]
boxxy = cell2mat(arrayfun(@(sz) rand(1, 2) .* (bottomright - topleft - sz) + topleft, boxsize, 'UniformOutput', false))
array2table([boxxy boxsize], 'VariableNames', {'X', 'Y', 'Size'})
Thank you very much Guillaume. I appreciate very much the help you have given me!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!