简体   繁体   English

randomGenerator避免连续重复

[英]randomGenerator avoid duplicates in a row

I have a question regarding the following code 我对以下代码有疑问

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];

This code works fine however I would like to know if it was possible to get it to not pick the same word two times in a row. 这段代码工作正常,但我想知道是否有可能让它不连续两次选择相同的单词。 For example, if it just selected "Red" then it would not pick "Red" again the next consecutive time. 例如,如果它只选择了“红色”,那么它将不会在下一个连续时间再次选择“红色”。 I read something about DISTINCT but I'm not sure if that's along the right path. 我读了一些有关DISTINCT的内容,但我不确定这是否正确。

Here is the code for the button which uses this 这是使用它的按钮的代码

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

Thankyou for your help 谢谢您的帮助

go for list and remove color once used: 去列表并删除一旦使用的颜色:

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

You can do this in two way (probably more but two that I can think of right now): 你可以用两种方式做到这一点(可能是我现在能想到的两种方式):

1) Create a function that uses a global variable to store the last generated random number. 1)创建一个使用全局变量存储最后生成的随机数的函数。 It will look something like this: 它看起来像这样:

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

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

  lastRandGenerated = aux;

  return aux;
}

, where lastRandGenerated is a global variable that you initialize to 0. ,其中lastRandGenerated是一个初始化为0的全局变量。

Then you use this function to generate random numbers. 然后使用此函数生成随机数。

2) You can create a class that has a function very similar to the one above and then instantiate an object of that class and use that to generate your random numbers. 2)您可以创建一个具有与上述函数非常相似的函数的类,然后实例化该类的对象并使用它来生成随机数。 In the class create a static variable that will remember the last generated random number. 在类中创建一个静态变量,它将记住最后生成的随机数。 Use that instead of the global variable. 使用它而不是全局变量。

The specifics are a bit out of my league but as a math problem there are 24 combinations (4 * 3 * 2 * 1). 具体情况有点超出了我的联盟,但作为一个数学问题,有24种组合(4 * 3 * 2 * 1)。 As heavy handed as this might sound, worst case you could work out all the combos and then pick a one out of 24 random. 尽管这听起来很重,但最糟糕的情况是你可以解决所有的组合,然后随机选择一个中的一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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