Как сделать чтобы Random не повторялся
TextView resText2 = (TextView) findViewById(R.id.textView2);
r = new Random();
output = r.nextInt(5) +1;//если 0 +1
resText2.setText("" + output);
Не работает
Set<Integer> existing = new HashSet();
public void onClick_Start(View view) {
TextView resText2 = (TextView) findViewById(R.id.textView2);
r = new Random();
output = r.nextInt(5) +1;//если 0 +1
do {
output = r.nextInt(5) +1;
} while(existing.contains(output));
existing.add(output);
resText2.setText("" + output);
}
Ответы (1 шт):
Автор решения: Komdosh
→ Ссылка
Ну можно так
Set<Integer> existing = new HashSet(); // где-нибудь за пределами метода
do {
output = r.nextInt(5) +1;
} while(existing.contains(output));