lundi 3 avril 2017

Java How select random number within a range without duplicate

I want to setVisible(false) elements in an array randomly. But when write this code

int random = (int) (Math.random() * 24 + 1);

myLabel.get(random).setVisible(false);

sometimes labels are not be unvisible. Because random gives same number. I do not mean how to generate number without duplicated. Because even the numbers are not duplicated, random may chooses same number twice or more.For example;

Let's say i generate 1 to 5 ints without duplicate like {2,4,5,1,3}. When i try to choose randomly, the program may choose 4 twice and maybe its never choose 3 for 5 choices.

I hope i can write it properly and clear.




Aucun commentaire:

Enregistrer un commentaire