MATLAB: Randomize set of numbers but don’t want more than 2 repeats in a row

randomize

I am trying to randomize a set of 3 numbers (1,2,3), each repeated 40 times. So im looking to make a 120×1 matrix, with the numbers 1,2,3 randomized and repeated 40times each. However, I want to pseudorandomize them so that alike numbers are not next to each other more than twice. Is there a way to do this?

Best Answer

a=[];
for k=1:40
a=[a;randperm(3)'];
end
Related Question