Random without repetition

3

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);

        } 

    });

}

}

    
asked by anonymous 11.10.2017 / 16:10

3 answers

1

Do this:

private static ArrayList<Integer> repeat_list = new ArrayList();

private int inicial;
private int finall;

private int mostrarresultado() {                    
    inicial = editTextInicial.getValue();
    finall = editTextFinal.getValue();
    Random Random = new Random();
    int Resultado = Random.nextInt(finall + 1 - inicial) + inicial;  

    return  Resultado;                      
}


private void verifyRepeated(int removeValue)
{
    int getParams = removeValue;
    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: " + removeValue.toString(), 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);

} 
    
11.10.2017 / 16:24
6

Try to store your values in a Set<Integer> . The Set data type is a list that does not store repeated values. This will ensure that the stored values are not repeated.

PS: Integer is a wrapper class of primitive type int .

    
11.10.2017 / 16:18
3

You should use the Collections.shuffle () command, so it will always be a random number and will not repeat, for example:

ArrayList<Integer> number = new ArrayList<Integer>();
for (int i = inicial; i <= finall; ++i){
    number.add(i);
}
Collections.shuffle(number);
    
11.10.2017 / 16:16