3
Well, I have a question that’s been killing me for a long time, and I can’t fix it. PS: I’m a beginner on Android.
I am developing an application of raffle, but I am not able to generate a Random without repeating the numbers already drawn, I tried everything, but still it repeats the number.
Follows 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);
}
});
}
}
That’s the best answer. In a college job, I needed to generate a very large amount of random numbers. By listing, duplicate queries took a long time because they were(n). After I switched to a Set, which is always (1), the program took off..
– Vítor Martins
Can you give me an example of how I do it?
– J.Silva
@J.Silva you need to create a data collection of the type
Set<int>which is aCollectionas well asList<>, and in this collection you add call returns to your methodmostrarresultado(). This ensures that returns do not recur, not to mention that the implementation and even the complexity of your algorithm will be much simpler. Example:Set<int> resultados = new HashSet<int>();resultados.add(mostrarresultado());– Wellington Costa
Set<Integer>and notSet<int>- primitive types cannot be used as generics in Java.– Victor Stafusa
@Victorstafusa is. I didn’t pay attention to this detail.
– Wellington Costa