Random Numbers in Android Studio

Asked

Viewed 4,208 times

0

This code is generating random numbers in 6 TextViews but I can’t make a code to prevent these numbers from repeating themselves.

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

    }
}

3 answers

1

There are several ways to do it. You can try using a Hashset, which is a collection of objects without repetition.

Before assigning the values in the textviews, store in the Hashset, because the Hashset add method adds an item only if it does not exist, then you can create a loop that infinitely adds a random number in the Hashset while the total size does not reach 6.

For example:

HashSet hs = new HashSet ();

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

Then you take the value of each Hashset item (using an Iterator) and assign it to a Textview:

Iterator iterator = hs.iterator(); 

mytext.setText("" + iterator.next());
mytext2.setText("" + iterator.next());
// ...

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 to generate numbers without repetition with a large interval (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 remove the numbers from this list randomly to ensure that the next numbers are not repeated. How to draw cards from a deck!

I hope I’ve helped :)

0

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

  1. First you create an integer vector of size 60.
  2. Fills every vector with positions from 0 to 59.
  3. Use the method Collections.shuffle to shuffle the values;
  4. Take the top 6 positions. ## or if you prefer, see more.

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 each TextView you enter a value at its 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]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.