# generate two variable from distribution without same result

2 views (last 30 days)
mingcheng nie on 11 Jan 2023
Edited: Bruno Luong on 11 Jan 2023
Hi there, if we have two variables are generated from uniform distribution as : A=randi([0,5],1,6); B=randi([-4,4],1,6);
Now I want some simple steps that can make: A and B are both vector, and I define that C(i)=[A(i) B(i)], i=1:6; then I dont want any of rows of C are the same. How should I do for the randi to make this happen?
##### 2 CommentsShowHide 1 older comment
mingcheng nie on 11 Jan 2023
Sorry for my bad explanation, here I give an example:
A=[1 2 3 3 2 3];
B=[1 2 4 5 1 4];
we can observe that in the third and sixth postion, AB have the same values, I don't want this happen. But in the first and fifth postion, B's elements are same but A's elements are not, then this is fine. Only remove that both when A's and B's elements are the same and generate new value

Walter Roberson on 11 Jan 2023
one of the ways is to prevent it from happening at all, by excluding the random A from the values available to be chosen from.
Achoices = 0:5;
numA = length(Achoices);
Bchoices = -4:4;
for row = 1 : 6
A = Achoices(randi(numA));
availableB = setdiff(Bchoices, A);
B = availableB(randi(numel(availableB)));
C(row,:) = [A, B];
end
Another way is to generate 6 entries freely without any conflict control, and then to remove rows that have duplicates, and then go back and generate as many more entries as you need. You would want a loop for this, each time generating (6 minus the number already generated), as it is possible for the new attempts to also happen to have conflicts.
Oh way... are you asking that the A and B entries of any one row are not the same, or is that acceptable but no two rows of C can be the same as other rows? If so then the loop generating some and removing duplicates and going back to generate any missing ones works fine.
Sometimes people improve the random efficiency by generating more entries than they need, expecting that some will probably clash, and hoping that enough non-clash will be left over after removing duplicates. You generally still need some looping if you do that, just not as many loop trips on average.
mingcheng nie on 11 Jan 2023
thank you for your answer, I give an example in the commet to make my question more clear

Bruno Luong on 11 Jan 2023
Edited: Bruno Luong on 11 Jan 2023
Warranty never repeated pairs
lA = 0; uA = 5; % lower upper bound of A
lB = -4; uB = 4; % and B
n = 50; % number of pair elements to be generated, 6 in your case
mA = (uA-lA)+1;
mB = (uB-lB)+1;
[iA,iB] = ind2sub([mA,mB], randperm(mA*mB,n));
A = lA-1+iA,
A = 1×50
4 3 4 1 3 0 2 1 1 3 3 0 5 3 0 0 0 4 3 0 4 1 4 2 5 0 1 5 5 1
B = lB-1+iB,
B = 1×50
4 2 0 -3 -4 -2 -3 2 -2 3 0 1 -2 4 2 -3 -4 -3 -2 0 3 1 -2 -1 -4 3 0 2 -3 -1

### Categories

Find more on Rayleigh Distribution in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!