Well I have a question that is killing me already has time, and I can not solve. PS: I'm a beginner on Android.
I'm developing a lottery application, but I can not generate a Random without repeating the numbers already drawn, I've tried everything, but still it repeats the number.
Follow the Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final NumberPicker InicialNP = (NumberPicker) findViewById(R.id.inicialID);
InicialNP.setMinValue(0);
InicialNP.setMaxValue(700);
InicialNP.setWrapSelectorWheel(true);
InicialNP.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal){
}
});
final NumberPicker FinalID = (NumberPicker) findViewById(R.id.finalID);
FinalID.setMinValue(0);
FinalID.setMaxValue(700);
FinalID.setWrapSelectorWheel(true);
FinalID.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal){
}
});
EditText Nomesorteio = (EditText) findViewById(R.id.nomesorteioID);
EditText Resultado = (EditText) findViewById(R.id.resultadoID);
Button Sortear = (Button) findViewById(R.id.btnsortear);
Sortear.setOnClickListener(new View.OnClickListener() {
private int mostrarresultado() {
inicial = InicialNP.getValue();
finall = FinalID.getValue();
Random Random = new Random();
int Resultado = Random.nextInt(finall + 1 - inicial) + inicial;
return Resultado;
}
private void verifyRepeated(int removeValue) {
int getParams = removeValue;
Random random = new Random();
getParams = random.nextInt(finall + 1 - inicial) + inicial;
while(true)
{
if(!repeat_list.contains(getParams))
break;
else
getParams = random.nextInt(finall + 1 - inicial) + inicial;
}
repeat_list.add(getParams);
Toast.makeText(MainActivity.this, "O valor sorteado foi: " + Integer.toString(removeValue), Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
list.clear();
click = MediaPlayer.create (MainActivity.this, R.raw.click);
click.start ();
Toast.makeText(MainActivity.this, "Sorteando", Toast.LENGTH_LONG).show();
int Resultado = mostrarresultado();
verifyRepeated(Resultado);
}
});
}
}