To solve this problem, use the four numbers 1, 2, 3, and 4 to arrange the numbers randomly. for example:
A total of 20 digits. do not repeat two adjacent numbers, for example, 3 or 3.
How do I write a function?
Reply to discussion (solution)
What does this mean?
$a = array(1,2,3,4);$b = array_merge($a, $a, $a, $a, $a);for($i=0; $i<1000; $i++) { shuffle($b); if(! preg_match('/(.)\1/', join('', $b))) printf("%d %s\n", $i, join('', $b));}
What does this mean?
$a = array(1,2,3,4);$b = array_merge($a, $a, $a, $a, $a);for($i=0; $i<1000; $i++) { shuffle($b); if(! preg_match('/(.)\1/', join('', $b))) printf("%d %s\n", $i, join('', $b));}
I need to generate an array with 20 elements. they are composed of four numbers: 1, 2, 3, and 4. the array elements are single numbers and do not repeat adjacent elements.
Did you see it? Isn't that what I mean?
I concatenate arrays into strings for convenience.
= 1 && $a[$count] == $a[$count-1]) {$a[$count]=mt_rand(1,4);check();}}for ($i=0; $i < 20; $i++) { $a[$i]=mt_rand(1,4);check();echo $a[$i]."
";}?>
Thank you for your answers.