【问题标题】:randomGenerator avoid duplicates in a rowrandomGenerator 避免连续重复
【发布时间】:2012-09-21 22:08:32
【问题描述】:

我对以下代码有疑问

Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(4);
    String wordList[] = new String[4];
    {
        wordList[0] = "Red";
        wordList[1] = "Blue";
        wordList[2] = "Green";
        wordList[3] = "Orange";


    }

String wordToDisplay = wordList[randomInt];

此代码工作正常,但我想知道是否有可能让它连续两次不选择同一个单词。例如,如果它只是选择了“红色”,那么它不会在下一个连续的时间再次选择“红色”。我读过一些关于 DISTINCT 的文章,但我不确定这是否正确。

这是使用此按钮的代码

final Button button1 = (Button) findViewById(R.id.button1);
        final TextView textView = (TextView) findViewById(R.id.text_random_text);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Random randomGenerator = new Random();
                int randomInt = randomGenerator.nextInt(9);
                String wordToDisplay = wordList[randomInt];
                textView.setText(wordToDisplay);

感谢您的帮助

【问题讨论】:

  • 这是在方法或循环内还是什么?
  • @IonutHulub 我有一个按钮使用字符串 String wordToDisplay = wordList[randomInt];
  • @IonutHulub 编辑了我的问题以添加更多代码
  • 我说的是int randInt = randomGenerator.nextInt(4) 行。

标签: android eclipse random duplicates


【解决方案1】:

查找列表并删除使用过的颜色:

private static ArrayList<String> arrayList = new ArrayList<String>();
private static Random random = new Random();
public static void fillList(){
    arrayList.add("Red");
    arrayList.add("Blue");
    arrayList.add("Green");
    arrayList.add("Orange");
}
public static String getNextRandomColor(){
    if(arrayList.isEmpty()){
        fillList();
    }
    return arrayList.remove(random.nextInt(arrayList.size()));
}

【讨论】:

  • 这是一个非常糟糕的答案,因为经过 4 次迭代,列表将是空的,他将无法再次获得一个词。
  • 我希望能够反复使用列表
  • @SudarNimalan 谢谢!这会改变我对按钮的实现吗?
  • 是的,你必须替换 textView.setText(wordToDisplay);与 textView.setText(getNextRandomColor());。并避免创建 Random randomGenerator = new Random();一次又一次地创建一个并使用 nextInt,这会给你更多的随机数。
  • @SudarNimalan 明白了!另外,我不明白您所说的避免创建 Random randomGenerator = newRandom(); 的意思
【解决方案2】:

您可以通过两种方式做到这一点(可能更多,但我现在能想到的两种方式):

1) 创建一个函数,该函数使用全局变量来存储最后生成的随机数。 它看起来像这样:

int myRand(int i) {
  int aux;
  Random randomGenerator = new Random();

  do {
    aux = randomGenerator.nextInt(i);
  } while (aux != lastRandGenerated);

  lastRandGenerated = aux;

  return aux;
}

,其中 lastRandGenerated 是您初始化为 0 的全局变量。

然后你使用这个函数来生成随机数。

2) 您可以创建一个具有与上述功能非常相似的类,然后实例化该类的一个对象并使用它来生成您的随机数。在类中创建一个静态变量,该变量将记住最后生成的随机数。使用它而不是全局变量。

【讨论】:

  • 这是我的第一个应用程序,所以我对此有点陌生。你能详细说明你的第一种方法吗?我了解创建全局变量,但我不明白如何将其链接到我的生成器
  • 您没有将它链接到生成器。您创建一个函数,该函数(在循环中)创建一个随机数并将其保存为aux,然后它检查它是否等于全局变量,如果是则意味着它必须再次生成一个数字。它这样做直到生成的数字与最后生成的数字不同,然后返回它。只需创建一个名为lastRandGenerated 的全局变量,将函数复制粘贴到您的程序中并像myRand(4) 一样调用它而不是randomGenerator.nextInt(4)
【解决方案3】:

细节有点超出我的范围,但作为一个数学问题,有 24 种组合 (4 * 3 * 2 * 1)。尽管这听起来很沉重,但最坏的情况是你可以计算出所有的组合,然后从 24 个随机中选择一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-11
    • 2013-06-30
    • 2013-12-18
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多