Aide sur Math.random???

newlc Messages postés 56 Date d'inscription samedi 24 juillet 2004 Statut Membre Dernière intervention 5 juillet 2010 - 8 févr. 2008 à 17:23
newlc Messages postés 56 Date d'inscription samedi 24 juillet 2004 Statut Membre Dernière intervention 5 juillet 2010 - 10 févr. 2008 à 20:57
salut à tous,

comment pourrais-je ecrire une commande "aléatoire"

je sais qu'il faut que j'utilise la fonction math.random mais ché pon bien l'ecrire en AS
pouvez vous m'aider????

pour lemoment j'ai un bouton suivant et prédédent qui fonctionne très bien, voici le code de ces 2 boutons:
//********************************************************************************
....
...chargement XML
....
btnNext.onRelease = function() {
    if (enCours.nextSibling==null) {
        enCours = premierContact;
    } else {
        enCours = enCours.nextSibling;
    }
    afficheContact(enCours);
};

btnBack.onRelease = function() {
    if (enCours.previousSibling==null) {
        enCours = dernierContact;
    } else {
        enCours = enCours.previousSibling;
    }
    afficheContact(enCours);
};
btnALEATOIRE.onRelease = function() {
???
}
//************************************************************************
A voir également:

4 réponses

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
8 févr. 2008 à 18:40
/*
RandomRange

parametres :
  min [I] number, borne minimale
  max [I] number, borne maximale

retour :
  number, nombre aleatoir entre min et max (bornes incluses)

*/


function RandomRange(min:Number, max:Number):Number {
   return(Math.floor(Math.random() * (max - min + 1)) + min);
}

// test 100 nombres aleatoire de 1 a 100
for (i=0; i<100; i++)
  trace(randomrange(1,100));





<hr size="2" width="100%" />


http://deefaze.gnomz.com
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
8 févr. 2008 à 18:56
function randomrange(min:Number, max:Number):Number {
   return(Math.floor(Math.random() * (max - min + 1)) + min);
}

function NextNode(node: XMLNode, defaut: XMLNode): XMLNode {
  return(node==null ? defaut : node.nextSibling);
}

function PrevNode(node: XMLNode, defaut: XMLNode): XMLNode {
  return(node==null ? defaut : node.previousSibling);
}
 
btnNext.onRelease = function() {
  enCours = NextNode(enCours, premierContact);
  afficheContact(enCours);
}

btnBack.onRelease = function() {
  enCours = PrevNode(enCours, dernierContact);
  afficheContact(enCours);
}

btnALEATOIRE.onRelease = function() {
   N = RandomRange(3,10);
   S = RandomRange(1,50);
   if (S > 25) {
     for (i=0; i==N; i++) {
       enCours = NextNode(enCours, premierContact);
     }
   } else {
     for (i=0; i==N; i++) {
       enCours = PrevNode(enCours, dernierContact);
     }
   }
   afficheContact(enCours);




<hr size="2" width="100%" />


http://deefaze.gnomz.com
0
newlc Messages postés 56 Date d'inscription samedi 24 juillet 2004 Statut Membre Dernière intervention 5 juillet 2010 1
10 févr. 2008 à 20:54
merci foxi pour ton coup de main,

seulement, j'ai saisie ton code mais ça ne fonctionne pas, j'ai pas d'erreur de flash mais l'affichage aléatoire ne marche pon, je te file tout le code pour que tu regardes:

 **************************************************debut code************


contacts_xml = new XML();
contacts_xml.ignoreWhite = true;
contacts_xml.onLoad = function(ok) {
 if (ok) {
  noeuds = this.firstChild.childNodes;
  nbrContacts = noeuds.length;
  premierContact = this.firstChild.firstChild;
  dernierContact = this.firstChild.lastChild;
  enCours = premierContact ;
  afficheContact(enCours);
 }
};
contacts_xml.load("xml/contact.xml");


function afficheContact(contact) {
 this.labelID.text=contact.childNodes[0].childNodes[0];


}
function randomrange(min:Number, max:Number):Number {
   return(Math.floor(Math.random() * (max - min + 1)) + min);
}


function NextNode(node: XMLNode, defaut: XMLNode): XMLNode {
  return(node==null ? defaut : node.nextSibling);
}


function PrevNode(node: XMLNode, defaut: XMLNode): XMLNode {
  return(node==null ? defaut : node.previousSibling);
}
 
btnNext.onRelease = function() {


   N = randomrange(3,10);
   S = randomrange(1,50);
   if (S > 25) {
     for (i=0; i==N; i++) {
       enCours = NextNode(enCours, premierContact);
     }
   } else {
     for (i=0; i==N; i++) {
       enCours = PrevNode(enCours, dernierContact);
     }
   }
   afficheContact(enCours);
}

*********************************************fin code**************

merci de ton aide!!
0
newlc Messages postés 56 Date d'inscription samedi 24 juillet 2004 Statut Membre Dernière intervention 5 juillet 2010 1
10 févr. 2008 à 20:57
merci foxi pour ton coup de main,

seulement, j'ai saisie ton code mais ça ne fonctionne pas, j'ai pas d'erreur de flash mais l'affichage aléatoire ne marche pon, je te file tout le code pour que tu regardes:

 **************************************************debut code************


contacts_xml = new XML();
contacts_xml.ignoreWhite = true;
contacts_xml.onLoad = function(ok) {
 if (ok) {
  noeuds = this.firstChild.childNodes;
  nbrContacts = noeuds.length;
  premierContact = this.firstChild.firstChild;
  dernierContact = this.firstChild.lastChild;
  enCours = premierContact ;
  afficheContact(enCours);
 }
};
contacts_xml.load("xml/contact.xml");


function afficheContact(contact) {
 this.labelID.text=contact.childNodes[0].childNodes[0];


}
function randomrange(min:Number, max:Number):Number {
   return(Math.floor(Math.random() * (max - min + 1)) + min);
}


function NextNode(node: XMLNode, defaut: XMLNode): XMLNode {
  return(node==null ? defaut : node.nextSibling);
}


function PrevNode(node: XMLNode, defaut: XMLNode): XMLNode {
  return(node==null ? defaut : node.previousSibling);
}
 
btnNext.onRelease = function() {


   N = randomrange(3,10);
   S = randomrange(1,50);
   if (S > 25) {
     for (i=0; i==N; i++) {
       enCours = NextNode(enCours, premierContact);
     }
   } else {
     for (i=0; i==N; i++) {
       enCours = PrevNode(enCours, dernierContact);
     }
   }
   afficheContact(enCours);
}

*********************************************fin code**************

merci de ton aide!!
0
Rejoignez-nous