how to generate a random sequence by excluding random number for each trial?

11 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2015-12-30
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by