简体   繁体   中英

How do can I get non-repeated random integers?

I have this Android application, it's very simple, basically I get a string from the database and then i press a button and get another one, except that I want to get random strings and not repeat them once they have been "used"

    public String getQuestion() {

        mydManager.openReadable();
        ArrayList<String> tableContent = mydManager.retrieveRows();

        int size = tableContent.size();

        Random rand = new Random();
        int rand_int = rand.nextInt(size);
        String a = tableContent.get(rand_int);

        String[] myarray = a.split("\n");
        String Rquestion = (myarray[1]);
        question = findViewById(R.id.question);
        question.setText(Rquestion);

        return a;
    }

Once i get the question with index 1 in the array, I do not want to get that same question again when calling this function

Instead of retrieving the rows everytime a new Question is generated, you could declare and initialize the ArrayList<String> as a global variable and after getting a random one, remove it from the array

private ArrayList<String> tableContent;
Random rand = new

public YourClass yourConstructor () { // or MainActivity initializer if android studio
        mydManager.openReadable();    
        tableContent = mydManager.retrieveRows();
}

public String getQuestion() {
        int size = tableContent.size();

        int rand_int = rand.nextInt(size);
        String a = tableContent.get(rand_int);

        //remove the element from arraylist
        tableContent.remove(a);

        String[] myarray = a.split("\n");
        String Rquestion = (myarray[1]);
        question = findViewById(R.id.question);
        question.setText(Rquestion);

        return a;
}

This should do the trick

Okay so as more questions are being added to your database, You cannot retrieve the list of questions from database just once. You have to do that on every button click. So now you have 2 options:

  • Maintain a list of used indices/numbers: when you generate your next random number, it should be different that these indices/numbers. If its the same then regenerate. Also, for this the questions being added/retrieved to/from database should not change order of the previous ones.

  • Delete the used question from the arraylist (not necessary if its not used elsewhere) and database

globle arraylist declare

 ArrayList<Integer> integers; // as globle

//onCreate

integers = new ArrayList<>();
public int gen(int i) {
        int alldata = i;
        Random rand = new Random();
        int rand_int = rand.nextInt(i);
        if (integers.size() == 0) {
            integers.add(rand_int);
        }else{
            if (integers.contains(rand_int)) {
                return gen(i);
            } else {
                integers.add(rand_int);
            }
        }
        return rand_int;
    }


int size = tableContent.size();
String a = tableContent.get(gen(size));

above code generate new integer number every time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM