Random java

Loula -  
 agh -
Bonjour a tous
J'ai un probleme en java
Je veux generer des chifffes aleatoires, mais le résultat est toujours le meme pourtant jinitialise aux nombres de secondes de la date courante :-| .voici mon bout de prog

public void actionPerformed(ActionEvent evt) {
for(int i=0;i<6;i++){
Random r = new Random();
Date date=new Date(30,12,2004);

int nbrPourInitialiser=date.getSeconds();
r.setSeed((long)nbrPourInitialiser);
System.out.println(nbrPourInitialiser);/*Le res donne toujours 0*/
int n= r.nextInt(49);
String s =""+n;
boutons[i].setText(s);
}

Merci a vous tous pour votre aide.
Bonne journee et bonne fete
A voir également:

3 réponses

zagor5 Messages postés 187 Date d'inscription   Statut Membre Dernière intervention   32
 
Pour prendre la date en court, il faut juste utiliser Date date=new Date(); .
Si ta boucle s'execute en moins d'une seconde, la valeur nbrPourInitialiser sera toujours la même.

Je te conseil de sortir de ta boucle l'initialisation du random.


public void actionPerformed(ActionEvent evt){
        Random r = new Random();
        Date date=new Date();
        
        int nbrPourInitialiser=date.getSeconds();
        r.setSeed((long)nbrPourInitialiser);
        System.out.println(nbrPourInitialiser);
        
        for(int i=0;i<6;i++){
            
            int n= r.nextInt(49);
            String s =""+n;
            boutons[i].setText(s); 
        }
    }


Plus ça rate, plus on a de chances que ça marche.
0
Loula
 
Merci beaucoup pour ta reponse cest tres gentil de ta part.
Bonne Année
0
agh
 
bonjour,

j'ai un probleme dans mon pg c ke quand j'affiche ma matrice de vector il me l'affiche aléatoirement c ca ke je veuw faire mais ya des objets ki manquent a chaque fois .

voici le bout de mon pg

x=r1.nextInt(10);y=r2.nextInt(10);
while ((compt<10) && (tab[x][y]== null)){


Vector fourmi=new Vector();



fourmi.addElement(new Integer(compt));
tab[x][y]=fourmi;compt=compt+1;
x=r3.nextInt(10);y=r4.nextInt(10);



}

for(int i=0;i<10 ;i++){
for(int j=0;j<10;j++){

System.out.println(tab[i][j]);
0