dimanche 26 avril 2015

How to stop duplicates when picking random number in Java

public void start() throws TwitterException, IOException {

    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
    AccessToken oathAccessToken = new AccessToken(ACCESS_KEY,
            ACCESS_KEY_SECRET);
    twitter.setOAuthAccessToken(oathAccessToken);
    ColourBlender myColourBlender = new ColourBlender();
    twitter.updateStatus(TwitterActions.getCatchphrase());
}

public static String getCatchphrase() throws FileNotFoundException
{
    ColourBlender myColourBlender = new ColourBlender();
    String newColour = myColourBlender.BlendColour();
    String[] phraseArray = {"Phrase1", "Phrase2", "Phrase3"};
    Random r = new Random();
    String catchphrase = phraseArray[r.nextInt(phraseArray.length)];
    return catchphrase;
}

In this code I want to have many catchphrases in an array, which will be tweeted randomly on twitter, but I don't want them to be repeated. How can I stop r generating duplicates




Aucun commentaire:

Enregistrer un commentaire