speed up generate N unique random numbers code
<p>I've been asked to generate all possible combinations in the range AA0000-ZZ9999(let's call it <code>folio</code>), for each combination I also need a 8 digits unique ramdom number (let's call it <code>attcode</code>), can't be consecutive, I know that there are a lot of combinations and the process will be slow, but as I'm using the <code>rand</code> function, and I've to validate that every <code>attcode</code> has to be unique, it is making my code slower, so if is it possible (I know it is, just don't know how), give me recommendations 'bout how can I improve this in my code</p>

<pre><code>$alph = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1);
$code_cache = array();
foreach ($alph as $value1) {
  foreach ($alph as $value2) {
    for ($i = 0; $i &lt;= 9; $i++) {
        $n=$i;
        if($i&lt;10){
            $n="000".$i;
        }
        elseif($i&lt;100){
            $n="00".$i;
        }
        elseif($i&lt;1000){
            $n="0".$i;
        }
        $code = rand(10000000, 99999999);
        while(in_array($code, $code_cache)){
            $code = rand(10000000, 99999999);
        }
        $code_cache[]=$code;
        echo $value1.$value2.$n.'-'.$code.'&lt;br/&gt;';
    }
  }
}
</code></pre>
