在行中没有三次重复的洗牌数字

shuffle Numbers without triple Repeat in row

在不连续重复三次三次的情况下,用数字洗牌数组的最快方法是什么

shuffleX(array(1,2,1,2,1,2,1,2,1,2,1,2,1),2)

1,1,1,2,2,2,... 不好

1,1,2,2,1,2,... 好

1,2,1,2,1,2,... 好

function shuffleX($arr, $max=2){

 ...
} 
$arr=array(1,2,1,1,2,2);
shuffleX($arr,2);

这是我的肮脏解决方案 "while Check" https://3v4l.org/bFjZ5

function shuffleX($arr, $max=3){
    $raw=$arr;
    $x=0;
    $s=md5('random_string');
    while(1){$x++;
        shuffle($arr);
            //Build string out of Arr for repeating variables to check
            $f2=join($s,$arr);
            $break=true;

            foreach($raw as $k=>$v){
                //Check for repeating variables each Value (forward and Backward)
                $f=str_repeat($v.$s,$max);
                $fa=str_repeat($s.$v.'',$max);  

                if(preg_match('#'.$f.'#',$f2) OR preg_match('#'.$fa.'#',$f2)){
                    $break=false;
                }

        }
        if($break===true){return $arr;}
        if($x>(count($arr)*1000)){// if nothing found after x Times break or give an empty array
            return array('unpossible');
        }
    }

}
$arr=array(1,2,1,1,2,2,1,2,1,2,1);
print_r(shuffleX($arr,3));