# how can i have random numbers with criteria?

2 views (last 30 days)
itay on 8 Jan 2015
Commented: itay on 9 Jan 2015
i need to have a 50 numbers matrice, while the numbers are between 1 to 5, and i need the neighbouring numbers to be different from each other. for example: 1 - 3 - 4 - 2 - 1 - 2 - 5 - 3 - 4 - 2...
i use:
r = [mod(randperm(50),5)+1];
but it gives me something like this: 1 - 2 - 4 - 4 - 3 - 4 - 5 - 5 - 5 - 3 - 1 - 2 - 2 - ....
how can i make the numbers to be different from the numbers before and after them?
thank you.

Guillaume on 8 Jan 2015
Pick any of the 5 numbers for the first elements, for the subsequents, pick any of the four numbers making the set difference between 1:5 and the previous number:
r = [randi(5) randi(4, 1, 49)]; %number 2-50 are indices of which of the four numbers to pick in setdiff.
for idx = 2:numel(r)
rr = setdiff(1:5, r(idx-1)); %exclude previous number
r(idx) = rr(r(idx));
end
Image Analyst on 8 Jan 2015
Uh yeah, dopey me (sound of hand slapping forehead).

John D'Errico on 8 Jan 2015
Edited: John D'Errico on 8 Jan 2015
Easy peasy, and vectorized too. No setdiffs needed, no loops, no tests. So it will be quite fast. And essentially one line of code (if we ignore the fact that I defined n and k separately for clarity.)
For the numbers to be different from each other in sequence, this just means that each element must have a difference with its neighbors that is non-zero, modulo 5. I suppose you could also write it as a Markov process, where the state transition matrix is a simple one...
T = (ones(5) - eye(5))/4
T =
0 0.25 0.25 0.25 0.25
0.25 0 0.25 0.25 0.25
0.25 0.25 0 0.25 0.25
0.25 0.25 0.25 0 0.25
0.25 0.25 0.25 0.25 0
Anyway, the simple solution is to pick the first element randomly, then pick a set of random differences, doing arithmetic mod 5. I'll write it for a general set of n numbers, each of which must lie in the set of integers [1:k], but with no consecutive elements that are the same. For your problem, n=50, k=5.
n = 50;
k = 5;
r = 1 + mod(cumsum([randi(k),randi(k-1,[1,n-1])]),k);
r
r =
Columns 1 through 24
4 2 1 3 1 5 2 3 5 4 1 4 2 4 5 2 5 4 1 5 4 3 2 3
Columns 25 through 48
1 2 3 4 5 2 4 3 1 4 1 5 3 4 5 1 2 4 1 2 3 4 5 4
Columns 49 through 50
3 2
As you can see by looking at the difference vector, there are no zero differences.
diff(r)
ans =
Columns 1 through 24
-2 -1 2 -2 4 -3 1 2 -1 -3 3 -2 2 1 -3 3 -1 -3 4 -1 -1 -1 1 -2
Columns 25 through 48
1 1 1 1 -3 2 -1 -2 3 -3 4 -2 1 1 -4 1 2 -3 1 1 1 1 -1 -1
Column 49
-1
And the min and max elements are 1 and 5 respectively.
max(r)
ans =
5
min(r)
ans =
1
itay on 9 Jan 2015
thanks :]

Edited: Isabella Osetinsky-Tzidaki on 8 Jan 2015
r=nan(50,1); rng('shuffle') r(1)=randi([1,5]); n=1; while n<50, rng('shuffle') p = randi([1,5]); if p~=r(n), n=n+1; r(n)=p; end end
John D'Errico on 8 Jan 2015
Please use the code formatting button, else your answer is unreadable, just a long strung out mess. Just select your text, and click on the little button that says "{} Code".