1
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;
}

在这段代码中,我希望在一个数组中有许多流行语,它们将在 Twitter 上随机发布,但我不希望它们重复。

如何停止 r 生成重复项?

4

4 回答 4

3

你可能只是洗牌phraseArray。所以元素将是随机顺序的。

Arrays.shuffle(phraseArray);
于 2015-04-26T18:06:26.723 回答
1

实现它的最简单方法是创建一个可能的数字列表(例如 1 到 100),然后您可以使用Collections.shuffle.

或者您可以使用LinkedHashSet来跟踪生成的数字

Random r = new Random(); 
Set<Integer> g = new LinkedHashSet<Integer>();
while (g.size() < numbersRequired)
{
    Integer n = r.nextInt(max) + 1;
    // Here it will automatically do a containment check
    g.add(n);
}

LinkedHashSet 将帮助您跟踪创建的数字的插入顺序。

于 2015-04-26T18:11:29.923 回答
0

添加一个 PhraseManager 从短语列表中删除使用的元素进行 N 次迭代(或其他一些机制,例如从不允许重复)怎么样。代码如下所示:

public class Class {
    static class PhraseManager {
        public final int N = 1;
        private Random r = new Random();

        private final LinkedList<String> phrases = new LinkedList<String>();
        private final LinkedList<String> phrasesUsedLastNTimes = new LinkedList<String>();

        public PhraseManager(Collection<String> phrases) {
            this.phrases.addAll(phrases);
        }

        public String getRandomPhrase() {

            String catchphrase = phrases.remove(r.nextInt(phrases.size()));
            phrasesUsedLastNTimes.addFirst(catchphrase);
            if(phrasesUsedLastNTimes.size() > N) {
                phrases.addLast(phrasesUsedLastNTimes.pollLast());
            }
            return catchphrase;
        }


    }

    private static PhraseManager phraseManager = new PhraseManager(Arrays.asList(new String[] {"Phrase1", "Phrase2", "Phrase3"}));

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


        return phraseManager.getRandomPhrase();
    }
} 
于 2015-04-26T18:52:32.907 回答
0

您不能保证与java.util.Random.nextInt(). 但是,如果您要生成的随机数相对较少,您可以将它们添加到集合中,然后执行Collections.shuffle().

于 2015-04-26T18:15:25.677 回答