Probleme php (commande rand)

codeur -  
 Zwerty -
bonjour,

je suis un créateur de site et je suis bloqué car je doit faire des nombres au hasard donc je fait ce code.
<?php
srand();
$a = rand(1, 10);
$b = rand(1, 10);
$c = rand(1, 10);
$d = rand(1, 10);
$e = rand(1, 10);
$f = rand(1, 10);
$g = rand(1, 10);
$h = rand(1, 10);
$i = rand(1, 10);
$j = rand(1, 10);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
   <head>
       <title>Liberté, Responsabilité et Outils de Communication</title>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="refresh" content="5; URL=index.php?p=1&q1=<?php echo $a; ?>&q2=<?php echo $b; ?>&q3=<?php echo $c; ?>&q4=<?php echo $d; ?>&q5=<?php echo $e; ?>&q6=<?php echo $f; ?>&q7=<?php echo $g; ?>&q8=<?php echo $h; ?>&q9=<?php echo $i; ?>&q10=<?php echo $j; ?>">
	   </head><body></body></html>

voila donc jenvoi tout par des variable sur la page index.php.

mon probleme: c que j'arive a avoir $b=6 et $j=6 et donc ca ne vat.

moi il me faut que toute les variable arrive a avoir un nombre unique.

merci d'avance pour vos reponse futur

1 réponse

  1. Zwerty
     
    voici une solution :)

    <?php  
    function nombres_uniques($min, $max)  
    {  
      $tableau = range($min, $max);  
      $tab_ret = array();  
      $keys = array_rand($tableau, $max);  
      foreach($keys as $key)  
       $tab_ret[] = $tableau[$key];  
      return $tab_ret;  
    }  
       
    $tab=nombres_uniques(1,10);  
    
    //vos affectations  
    $a = $tab[0];  
    $b = $tab[1];  
    $c = $tab[2];  
    $d = $tab[3];  
    $e = $tab[4];  
    $f = $tab[5];  
    $g = $tab[6];  
    $h = $tab[7];  
    $i = $tab[8];  
    $j = $tab[9];  
    
    //juste pour l'affichage :p  
    $variables= array("a","b","c","d","e","f","g","h","i","j");  
    for($i=0;$i<10;$i++)  
    {  
    echo $variables[$i]." = ".$tab[$i] . "<BR>";  
    }  
    ?>
    1