Generate n random numbers between 0.1 and 0.9 without repetition
12 views (last 30 days)
Show older comments
Hi all,
I need to generate n random points
where
and
, excluding the point
. My attempt is the following:




%define the lower limit
lower_limit = 0.1;
%define the upper limit
higher_limit = 0.9;
%No of random points
No_pts = 20;
%excluding value
No_value = 0.5
%Generate
valid_vals = setdiff(lower_limit:higher_limit, No_value);
result = valid_vals( randi(length(valid_vals), No_pts, 2) );
When running this code I only get one value which is
. Any idea how this can be modified to work properly?

Thanks.
0 Comments
Accepted Answer
Steven Lord
on 2 May 2025
When you compute:
lower_limit = 0.1;
%define the upper limit
higher_limit = 0.9;
x = lower_limit:higher_limit
The vector x contains the values from lower_limit to higher_limit using a step size of 1. How many times do you need to add 1 to 0.1 to reach 0.9 (if you go past 0.9, remove the points past 0.9, leaving only the points that are less than or equal to 0.9)?
If you want to use a smaller step size, specify it when you create the vector.
step_size = 0.1;
x = lower_limit:step_size:higher_limit
As for the rest of your code, you don't want to use randi for this application. randi generates random numbers with replacement, like you were rolling a die. The randperm creates a random permutation of numbers between 1 and the limit you specify as input, as though you were shuffling a deck of cards. Assuming a standard deck, the Queen of hearts (for one example) can only be in one place, but if I roll a six-sided die four times I could end up with two 3's, a 5, and a 6.
dieSides = 6;
numRolls = 4;
dieRolls = randi(dieSides, 1, numRolls)
deckSize = dieSides;
numCards = numRolls;
cardOrder = randperm(deckSize, numCards)
0 Comments
More Answers (2)
Thorsten
on 2 May 2025
Edited: Thorsten
on 2 May 2025
This generates No_pts distinct random points [x, y] between lower_limit and higher_limit, excluding the point [0.5, 0.5]:
%define the lower limit
lower_limit = 0.1;
%define the upper limit
higher_limit = 0.9;
%No of random points
No_pts = 20;
R = [];
while size(R, 1) < No_pts
R = lower_limit + (higher_limit - lower_limit)*rand(No_pts, 2);
R = unique(R, 'rows');
R = setdiff(R, [0.5, 0.5], 'rows');
end
1 Comment
Image Analyst
on 2 May 2025
Since rand() will always produce unique numbers, there is no need for the last two lines
% R = unique(R, 'rows');
% R = setdiff(R, [0.5, 0.5], 'rows');
It was hinted at, but not explicitly stated, that the numbers should all be rounded to the nearest 0.1. In that case the last two lines could be used:
R = [];
while size(R, 1) < No_pts
R = lower_limit + (higher_limit - lower_limit)*rand(No_pts, 2);
R = round(R, 1); % Round to nearest 0.1
% Make sure there are no duplicate rows.
R = unique(R, 'rows');
% Make sure 0.5, 0.5 is not one of the rows.
R = setdiff(R, [0.5, 0.5], 'rows');
end
John D'Errico
on 2 May 2025
There are always many ways to solve a problem. Here is mine:
% generate the set of ALL possible combinations. There are not many here,
% and that makes it terribly easy.
% Also, integers at first. this avoids any issue with floating point numbers.
[xset,yset] = meshgrid(1:9);
xyset = [xset(:),yset(:)];
% exclude the point you don't wish to see, and then divide by 10.
xyset = setdiff(xyset,[5,5],'rows')/10;
Now that we have the complete popiulation of point we will ever allow, we can choose our sample.
There will be 80=9*9-1 of them in the complete population. This also makes it very ovbious that you CANNOT generate more than 80 samples, if you will sample without replacement. The sampling will be done using randperm.
n = 10;
xy = xyset(randperm(size(xyset,1),n),:)
You shoiuld understand that because randperm was used, the result will be assured to be theoretically uniform in distribution, as well as always avoiding the point [0.5,0.5] by design.
This scheme works perfectly as long as you can generate the entire population easily.
0 Comments
See Also
Categories
Find more on Random Number Generation 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!