简体   繁体   English

如何从各种元素中随机选择

[英]How to randomly choose from a varying array of elements

my question is pretty simple in the concept : I have an array of 24 booleans which I've set all to false initially. 我的问题在概念上很简单:我有一个24个布尔值的数组,最初将它们都设置为false。 I can choose randomly an element of this array with the code: 我可以使用以下代码随机选择此数组的元素:

Random r = new Random();
int i1=r.nextInt(24);

Now from time to time I change the value of some elements to true, let say to the element number 14 and 22. How can I choose now randomly only between the other elements who are still false and not consider the one who are true? 现在,我不时地将某些元素的值更改为true,比方说元素编号14和22。我现在如何只在仍然为假的其他元素之间随机选择,而不考虑为真的元素呢? Thank you in advance for the answer. 预先感谢您的回答。

Edit : The code who works was : 编辑:谁工作的代码是:

Random r = new Random ();
int rand = r.nextInt (24);

while (array[rand]) {
          rand = r.nextInt (24);

        }

You can keep track of the indices in which the elements have the value of true in an ArrayList for example. 例如,您可以跟踪ArrayList元素的值为true的索引。 When generating a random number, check whether it's equal to one of the already true numbers (which should be excluded from the random number generation/selection) and re-generate a random number, until you get one of the array indices which is still at false . 生成随机数时,请检查其是否等于已经为true数字之一(应从随机数生成/选择中排除),然后重新生成一个随机数,直到获得仍然位于的数组索引之一false

int numberTrue=0; //adjust this in your other code accordingly
Random r = new Random ();
int rand = r.nextInt (24);
// loop until not true found or stop if all items are true
while (array [rand] && numberTrue < 24) {
  rand = r.nextInt (24);
  count++;
}

I thought about it for a bit, and without knowing your code here are a couple of suggestions to avoid that infinite loop when they are all true (assuming that condition may exist): 我考虑了一下,在不了解您的代码的情况下,有几点建议可以避免在它们全部成立时避免无限循环(假设条件可能存在):
1. keep a count of how many true are set (example adjusted for this) 1.记下设置了多少个true(为此示例进行了调整)
2. Store a second array of the true indexes, as suggested in the other answer 2.存储另一个真实索引数组,如其他答案所示

The first is probably the simplest to implement (adding a numberTrue++; and numberTrue--; in the appropriate code spots) - just a note here that if it's not possible to keep this count, you can achieve the same thing by running an initial for loop over your array and counting the number of true elements. 第一个可能是最简单的实现(在适当的代码段中添加一个numberTrue++;numberTrue--; )- 在此处仅需注意,如果无法保持此计数,则可以通过为遍历数组并计算真实元素的数量。
The second way would lend itself to utilizing one of the answers from the other post: 第二种方法是利用另一篇文章的答案之一:

public int generateRandom(int start, int end, ArrayList<Integer> excludeRows) {
    Random rand = new Random();
    int range = end - start + 1;

    int random = rand.nextInt(range) + 1;
    while(excludeRows.contains(random)) {
        random = rand.nextInt(range) + 1;
    }

    return random;
}

(I believe their code should actually read rand.nextInt(range) + start (我相信他们的代码实际上应该读rand.nextInt(range) + start
So you would run: 因此,您将运行:

if (trueIndexes.length < 24) {
      generateRandom(0, 23, trueIndexes);
    }

您可以创建具有“ false”的布尔元素列表,并从列表中随机选择元素。

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

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