MATLAB: How to generate a random sequence by excluding random number for each trial

hi
i want to generate two random numbers in between sequence 1 to 5. like 1 and 3, or 2 and 5, or 1 and 4. but the sequence is subject to one condition. which is like, generate two random numbers from 1 to 5 by excluding one random number. for e.g.
if x=3
it means generate two numbers in between [1 2 4 5] but not 3 . 3 is excluded from the choice. the result must be like
1 and 4. or 2 and 5 etc
Kindly help me 🙂
with best regards mudasir ahmed

Best Answer

I show both cases, where it's totally random so that the number may possibly repeat, and pseudo random where no repeat is allowed. Not sure which you wanted so I had to do both:
% Get two numbers in range with possible repeats.
iMin = 1;
iMax = 5;
% Get 100 numbers between iMin and iMax
r = randi([iMin, iMax], 100);
% Get rid of one certain number

numberToRemove = 3;
r(r==numberToRemove) = []; % r is smaller now.

% Now get two numbers from that by taking the first two.

r = r(1:2)
% Get two numbers in range without any repeats.
iMin = 1;
iMax = 5;
numToTake = 2; % Must be less than iMax - iMin
% Get (iMax-iMin) numbers between iMin and iMax
r = randperm(iMax-iMin) + iMin;
% Get rid of one certain number
numberToRemove = 3;
r(r==numberToRemove) = []; % r is smaller now.
% Now get two numbers from that by taking the first two.
r = r(1:2)