0

I'm creating a random quote generator but i'm having an issue in the fact that the quotes repeat too often is there anyway to make them non-repeating?

Here's my MainActivity, thanks in advance for your help

public class MainActivity extends AppCompatActivity {

    private Button addButton;    
    private TextView displayMessageTextView;
    private ArrayList<String> quotes = new ArrayList<String>();
    private Random randomGenerator = new Random();
    private int previousNumber = 0;

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

        quotes.add("You smart! You loyal! You're a genious!");
        quotes.add("I appreciate you");
        quotes.add("This is a major key");
        quotes.add("They will try to close the door on U, just open it");
        quotes.add("The key to more success is CoCo butter");
        quotes.add("Congratulations, you played yourself");
        quotes.add("Another one, no. Another two");
        quotes.add("I changed... a lot");
        quotes.add("They don't want you to jet ski");
        quotes.add("The key is to have every key");
        quotes.add("Almond milk + Cinnamon Toast Crunch = Major key to success");
        quotes.add("Do you see that bamboo? Ain't nothing like bamboo. Blessup");
        quotes.add("Bless up. Egg whites, Turkey bacon, Hashbrown, Water");
        quotes.add("They wanna come stress me out? Heh, bye");
        quotes.add("Lion Order");
        quotes.add("Watch your back, but when you get out of the shower dry your back, it's a cold world");
        quotes.add("Some of ya'll crabs");
        quotes.add("ANOTHA ONE");
        quotes.add("We jus seen 2 dolphins");
        quotes.add("They don't want you to win");
        quotes.add("Be A Star. Be A Superstar");
        quotes.add("I remember when I ain't have a jacuzzi");
        quotes.add("The other day the grass was brown, now it's green cuz I ain't give up");
        quotes.add("In life everyone has a choice. The key is...make a right choice");
        quotes.add("We have to get money. We have no choice. It cost money to eat");
        quotes.add("I love my bamboo trees. I love fruits. I love apples.");
        quotes.add("I told y'all this before, when you have a swimming pool do not use chlorine, use salt water");
        quotes.add("The key is: never fold");
        quotes.add("major key, get a pedicure and manicure once a week");
        quotes.add("They dont want you to be healthy");
        quotes.add("To make it thru the jungle you're gonna have to sweat");
        quotes.add("They never said winning was easy");
        quotes.add("It's important to shape up your hedges. It's like getting a haircut");
        quotes.add("LIOOOOON");
        quotes.add("To succeed, you must believe. When you believe you will succeed. ");
        quotes.add("The key to success is to have a hammock");
        quotes.add("Some people can't handle winning. I can.");
        quotes.add("They don't want you to have lunch");
        quotes.add("It's not an easy road but give thanks to the road");
        quotes.add("The key to success is to have a lot of pillows. A lot.");

        displayMessageTextView = (TextView) findViewById(R.id.displayMessageTextView);
        displayMessageTextView.setText("Press the button to generate a random quote");

        addButton = (Button) findViewById(R.id.addObjecttive);
        addButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int randomQuoteIndex = getRandomNumber();

                String quote = quotes.get(randomQuoteIndex);

                displayMessageTextView.setText(quote);
            }
        });
    }

    public int getRandomNumber() {
        int randomNumber = randomGenerator.nextInt(40);

        while (previousNumber == randomNumber) {
            randomNumber = randomGenerator.nextInt(40);
        }

        previousNumber = randomNumber;

        return randomNumber;
    }
}
Karen Forde
  • 1,117
  • 8
  • 20
  • take a look at `Collections.shuffle` instead of random number. See example here: http://stackoverflow.com/a/16000210/5318223 – timbre timbre Jul 08 '16 at 22:03

2 Answers2

0

There has been lots of discussion about this topic on stackoverflow:

Creating random numbers with no duplicates

In your case, the following approach may work too:

  • create a copy of your array quotes, e.g. quotesCopy
  • create a random number between 0 and 39
  • select an item from quotesCopy and remove it
  • create a random number between 0 and 38
  • ...
  • start at the beginning if quotesCopy becomes empty

EDIT:

Code cood look like this:

String returnRandomQuote(){
    if(quotesCopy==null || quotesCopy.size()==0){
        quotesCopy = (ArrayList<String>) quotes.clone();
    }
    Collections.shuffle(quotesCopy);
    String ret = quotesCopy.get(0);
    quotesCopy.remove(0);
    return ret;
}
Community
  • 1
  • 1
Christian Ammann
  • 888
  • 8
  • 19
0

First thing that comes to my mind is to create a copy of that list and remove every quote you displayed. When length of cpied array will be smaller than some hardcoded value you would recopy original arrray.

The second implementation that will work the same is to copy every chosen quote to some array and check after every random get if it exist in this array and if yes then try to get another one. If length of that array will be bigger than some hardcoded value than flush it.

EDIT:

You have to create another ArrayList. Then when you are taking any quote you check if it exists in it (The first one of course will not exist as it is empty array). If it does not exist you add it to your copy array. So it won't be chosen in a short time. When number of saved (displayed) quotes reches some value (here 20) you empty this array so the randomized choosing can start again will whle original list. If modified your code below. I'm no Java programmer but it should work.

public class MainActivity extends AppCompatActivity {

    private Button addButton;    
private TextView displayMessageTextView;
private ArrayList<String> quotes = new ArrayList<String>();
private ArrayList<String> quotes = new ArrayList<String>();
private Random randomGenerator = new Random();
private int previousNumber = 0;

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

    quotes.add("You smart! You loyal! You're a genious!");
    quotes.add("I appreciate you");
    quotes.add("This is a major key");
    quotes.add("They will try to close the door on U, just open it");
    quotes.add("The key to more success is CoCo butter");
    quotes.add("Congratulations, you played yourself");
    quotes.add("Another one, no. Another two");
    quotes.add("I changed... a lot");
    quotes.add("They don't want you to jet ski");
    quotes.add("The key is to have every key");
    quotes.add("Almond milk + Cinnamon Toast Crunch = Major key to success");
    quotes.add("Do you see that bamboo? Ain't nothing like bamboo. Blessup");
    quotes.add("Bless up. Egg whites, Turkey bacon, Hashbrown, Water");
    quotes.add("They wanna come stress me out? Heh, bye");
    quotes.add("Lion Order");
    quotes.add("Watch your back, but when you get out of the shower dry your back, it's a cold world");
    quotes.add("Some of ya'll crabs");
    quotes.add("ANOTHA ONE");
    quotes.add("We jus seen 2 dolphins");
    quotes.add("They don't want you to win");
    quotes.add("Be A Star. Be A Superstar");
    quotes.add("I remember when I ain't have a jacuzzi");
    quotes.add("The other day the grass was brown, now it's green cuz I ain't give up");
    quotes.add("In life everyone has a choice. The key is...make a right choice");
    quotes.add("We have to get money. We have no choice. It cost money to eat");
    quotes.add("I love my bamboo trees. I love fruits. I love apples.");
    quotes.add("I told y'all this before, when you have a swimming pool do not use chlorine, use salt water");
    quotes.add("The key is: never fold");
    quotes.add("major key, get a pedicure and manicure once a week");
    quotes.add("They dont want you to be healthy");
    quotes.add("To make it thru the jungle you're gonna have to sweat");
    quotes.add("They never said winning was easy");
    quotes.add("It's important to shape up your hedges. It's like getting a haircut");
    quotes.add("LIOOOOON");
    quotes.add("To succeed, you must believe. When you believe you will succeed. ");
    quotes.add("The key to success is to have a hammock");
    quotes.add("Some people can't handle winning. I can.");
    quotes.add("They don't want you to have lunch");
    quotes.add("It's not an easy road but give thanks to the road");
    quotes.add("The key to success is to have a lot of pillows. A lot.");

    displayMessageTextView = (TextView) findViewById(R.id.displayMessageTextView);
    displayMessageTextView.setText("Press the button to generate a random quote");

    addButton = (Button) findViewById(R.id.addObjecttive);
    addButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Boolean isDifferent = false;
            while(!isDifferent)
            {
                int randomQuoteIndex = getRandomNumber();
                String quote = quotes.get(randomQuoteIndex);
                if(!quotesCopy.contains(quote)) {
                    isDifferent = true;
                    quotesCopy.add(quote);
                }
                if(quotesCopy.size() > 20)
                    quotesCopy = new ArrayList<String>();
            }

            displayMessageTextView.setText(quote);
        }
    });
}

public int getRandomNumber() {
    int randomNumber = randomGenerator.nextInt(40);

    while (previousNumber == randomNumber) {
        randomNumber = randomGenerator.nextInt(40);
    }

    previousNumber = randomNumber;

    return randomNumber;
}

}