MATLAB: Random numbers without repetition of two consecutive numbers

MATLABrandom number generator

Hi everybody, I'm struggling with the randomization of the trials presented in my computerized task. I have to present a sequence of 5 numbers (range from 1 to 4) 100 times. I want that each row of the array contains 5 numbers without repeating two times consecutively the same number. Example :
%Wrong pseudorandom
1 2 2 1 4
3 3 4 1 2
1 4 4 4 2
%Right pseudorandom
1 2 3 1 4
2 3 4 1 2
1 4 2 4 3
Many thanks for any suggestions,
FM

Best Answer

result1=randperm(4);
result=[result1,result1(randi(3))]
Examples:
result =
4 1 3 2 1
result =
2 3 4 1 3
result =
4 2 1 3 2
result =
3 2 4 1 2
result =
3 2 1 4 2
result =
2 1 3 4 1
result =
2 3 4 1 3
result =
2 4 3 1 4
result =
2 1 3 4 1
result =
1 3 4 2 3
result =
2 3 1 4 3
Is it OK?