lundi 19 janvier 2015

android randon number between a range with no repeats

I want to make a simple app where there is a random number each time I start the activity, the random number should be between a range for example 4 and 10.


I find a way in this code but the problem is the range start from 0 only, how I can make it start from 1 or 2 to 4 instead of 0 to 4:



ArrayList<Integer> numbers = new ArrayList<Integer>();
Random randomGenerator = new Random();
while (numbers.size() < 4) {

int random = randomGenerator .nextInt(4);
if (!numbers.contains(random)) {
numbers.add(random);
}
}




Aucun commentaire:

Enregistrer un commentaire