samedi 30 septembre 2017

Get a random object with random value from a List in Java [duplicate]

This question already has an answer here:

i tried to get a random value from 50 to 100 of an object but i didn't find a solution! any help please?

public class OneArmedBandit implements Item {

Random rand = new Random();

private List<Integer> Item = new ArrayList<Integer>();

public OneArmedBandit(List<Integer> ItemList) {
    this.Item=ItemList;
}

public int AnyItem(List<Integer> ItemList) {

        Random rand = new Random();

        int index = ItemList.get(rand.nextInt(ItemList.size()));
        return ItemList.get(index);

}

public List<Integer> getListItem(){
    return this.Item;
}

}




Aucun commentaire:

Enregistrer un commentaire