I’m trying to generate an array of random numbers from 0-n then shuffle (but ensure that the keys and values DO NOT match).
For example:
0 => 3 1 => 2 2 => 4 3 => 0 4 => 1
Note that both keys and values are from 0-4 but none of the keys and values are the same.
Any thoughts?
Advertisement
Answer
$max = 5; $done = false; while(!$done){ $numbers = range(0, $max); shuffle($numbers); $done = true; foreach($numbers as $key => $val){ if($key == $val){ $done = false; break; } } }