<?php
//
// Auteur : Guillaume Sahut
// Site web : www.breakingcentral.com
//
// 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;
}
// 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
// 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);
// On visualise le résultat de ce magnifique code
// et on dit merci à BreakingCentral.com ^^
echo '<table>'."\n".'<tr><th>Clé</th><th>Valeur</th></tr>'."\n";
while (list( $key, $value) = each( $songs_list))
echo '<tr><td>'.$key.'</td><td>'.$value.'</td></tr>'."\n";
echo '</table>'."\n";
?>
|