I'm sharing a random generator between three methods:
private Random randomGen;
private List<String> list1;
private List<String> list2;
private List<String> list3;
public MyClass() { // this is the constructor
this.randomGen = new Random();
this.list1 = new ArrayList<>(Arrays.asList("one", "two", "three"));
this.list2 = new ArrayList<>(Arrays.asList("one", "two", "three", "four"));
this.list3 = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));
}
public int method1() {
return randomGen.nextInt(list1.size());
}
public int method2() {
return randomGen.nextInt(list2.size());
}
public int method3() {
return randomGen.nextInt(list3.size());
}
when I call each of the method
s, I get the same integer. The first time I call it is different, but each time after that it is the same:
myClassInstance.method1() // 1
myClassInstance.method2() // 1
myClassInstance.method3() // 1
/* next time */
myOtherClassInstance.method1() // 2
myOtherClassInstance.method2() // 2
myOtherClassInstance.method3() // 2
Why isnt it returning a different number each time?
Aucun commentaire:
Enregistrer un commentaire