<?php
// Fonction qui retourne un nombre entier aléatoire.
// Si $use_range vaut TRUE, la valeur renvoyée est
// comprise entre $from inclus et $to inclus.
// Si $use_range vaut FALSE, la valeur renvoyée est quelconque.
function get_random_integer( $use_range, $from, $to)
{
$from = (int) $from;
$to = (int) $to;
if ($use_range) $n = mt_rand( $from, $to);
else $n = mt_rand();
return $n;
}
// Fonction qui renvoie un nombre entier aléatoire, en testant
// éventuellement sa présence dans le tableau.
// Si $exclusive vaut TRUE, la fonction renvoie un nombre entier aléatoire,
// compris entre $from inclus et $to inclus, qui n'est pas déjà contenu
// dans le tableau $array.
// Si $exclusive vaut FALSE, la fonction renvoie un nombre entier aléatoire,
// compris entre $from inclus et $to inclus.
function get_random_integer_mode( $exclusive, $array, $from, $to)
{
do
{
$n = get_random_integer( TRUE, $from, $to);
}
while ( (in_array( $n, $array)) && ($exclusive));
return $n;
}
function get_random_integer_mode1( $exclusive, $array1, $from, $to)
{
do
{
$n1 = get_random_integer( TRUE, $from, $to);
}
while ( (in_array1( $n1, $array1)) && ($exclusive));
return $n1;
}
// On prépare le matos
$nb_songs = 10; // nombre de chansons
$nb_keys = $nb_songs - 1; // nombre de clés (de 0 à $nb_keys)
$songs_list = array(); // initialisation du tableau du résultat
$songs_list1 = array1(); // initialisation du tableau du résultat
// Tant que le tableau n'est pas complet, on le remplit.
while (!isset( $songs_list[ $nb_keys]))
$songs_list[] = get_random_integer_mode( TRUE, $songs_list, 1, $nb_songs);
$songs_list1[] = get_random_integer_mode1( TRUE, $songs_list1, 1, $nb_songs);
// On visualise le résultat de ce magnifique code
echo '<table>'."\n".'<tr><th>Clé</th><th>Valeur</th><th>Valeur1</th></tr>'."\n";
while (list( $key, $value, $value1) = each( $songs_list))
echo '<tr><td>'.$key.'</td><td>'.$value.'</td><td>'.$value1.'</td></tr>'."\n";
echo '</table>'."\n";
?>
|