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

4 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Image Analyst
Image Analyst le 30 Déc 2015
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)
  1 commentaire
Mudasir Ahmed
Mudasir Ahmed le 30 Déc 2015
thank you so much sir, i need second one :)
withe best regards mudasir

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by