Random numbers in Android Studio

0

This code is generating random numbers at 6% with% different, but I can not code to prevent these numbers from repeating.

package com.example.kelvin.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.*;
import android.content.*;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    public Button btnsim;
    public TextView texto1;
    public TextView texto2;
    public TextView Texto3;
    public TextView Texto4;
    public TextView Texto5;
    public TextView Texto6;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void exibir(View view){

                Random myRandom = new Random();
                Random myRandom2 = new Random();
                Random myRandom3 = new Random();
                Random myRandom4 = new Random();
                Random myRandom5 = new Random();
                Random myRandom6 = new Random();

                int number = myRandom.nextInt(60) + 1;
                int number2 = myRandom2.nextInt(60) + 1;
                int number3 = myRandom3.nextInt(60) + 1;
                int number4 = myRandom4.nextInt(60) + 1;
                int number5 = myRandom5.nextInt(60) + 1;
                int number6 = myRandom6.nextInt(60) + 1;

                TextView mytext = (TextView) findViewById(R.id.texto1);
                TextView mytext2 = (TextView) findViewById(R.id.texto2);
                TextView mytext3 = (TextView) findViewById(R.id.texto3);
                TextView mytext4 = (TextView) findViewById(R.id.texto4);
                TextView mytext5 = (TextView) findViewById(R.id.texto5);
                TextView mytext6 = (TextView) findViewById(R.id.texto6);

                String myString = String.valueOf(number);
                String myString2 = String.valueOf(number2);
                String myString3 = String.valueOf(number3);
                String myString4 = String.valueOf(number4);
                String myString5 = String.valueOf(number5);
                String myString6 = String.valueOf(number6);

                mytext.setText(myString);
                mytext2.setText(myString2);
                mytext3.setText(myString3);
                mytext4.setText(myString4);
                mytext5.setText(myString5);
                mytext6.setText(myString6);
        }

    public void naoexibir (View view) {

        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);

    }
}
    
asked by anonymous 09.05.2017 / 04:34

3 answers

1

Take a look at this post .

I'll translate here to make it easy:

public static int getRandomInt(int min, int max) {
    Random random = new Random();

    return random.nextInt((max - min) + 1) + min;
}

public static ArrayList<Integer> getRandomNonRepeatingIntegers(int size, int min,
        int max) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();

    while (numbers.size() < size) {
        int random = getRandomInt(min, max);

        if (!numbers.contains(random)) {
            numbers.add(random);
        }
    }

    return numbers;
}

And to receive 7 random numbers between 0 and 25:

ArrayList<Integer> list = getRandomNonRepeatingIntegers(7, 0, 25);
    for (int i = 0; i < list.size(); i++) {
        System.out.println("" + list.get(i));
    }

This method works well for generating non-repetition numbers with a large range (for example, between 0 and 1000).

For short interval cases (between 0 and 60) you can create a list containing the numbers (0,1,2,3,4 ... 60) and go randomly removing the numbers from this list to ensure that the next ones numbers are not repeated. How to draw cards from a deck!

I hope I have helped:)

    
09.05.2017 / 06:31
1

There are several ways to do it. You can try using a HashSet, which is a collection of non-repeating objects.

Before assigning the values in the textviews, save to HashSet because the HashSet add method adds an item only if it does not exist, so you can create a loop that infinitely adds a random number to the HashSet while the total size does not arrive to 6.

For example:

HashSet hs = new HashSet ();

do {
   Integer i = myRandom.nextInt(60) + 1;
   hs.add(i);
} while (hs.size() < 6);

Then you get the value of each item in the HashSet (using an Iterator) and assign it to a TextView:

Iterator iterator = hs.iterator(); 

mytext.setText("" + iterator.next());
mytext2.setText("" + iterator.next());
// ...
    
09.05.2017 / 06:31
0

There are several ways to do this, and one option is to use the shuffle of Collections . Here are the steps:

  • First you create a vector of integers of size 60.
  • Fills any vector with positions from 0 to 59.
  • Use the Collections.shuffle to shuffle the values;
  • Take the first 6 positions. ## or if you prefer, see you later.
  • See the method below:

    public static Integer[] numRandom(int qndNumbers){
        Integer[] arr = new Integer[60];
        Integer[] arrSelected = new Integer[6];;
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;          
        }
        Collections.shuffle(Arrays.asList(arr));
    
        for (int j = 0; j < qndNumbers; j++) {
            arrSelected[j] = arr[j]+1;
        }
        return arrSelected;
    }
    

    How to use:

    Integer[] valores  = numRandom(6);
    

    In this way, for every TextView you enter a value in your given position. Example:

    mytext1.setText(valores[0]);
    mytext2.setText(valores[2]);
    mytext3.setText(valores[2]);
    mytext4.setText(valores[3]);
    mytext5.setText(valores[4]);
    mytext6.setText(valores[5]);
    
        
    09.05.2017 / 06:40