简体   繁体   English

如何从数组中获取随机对象

[英]How to take random objects from an array

I am witing this program which removes 2 random objects at a time from a bag.我正在使用这个程序,它一次从袋子中取出 2 个随机物体。 Inside the bag there are objects of two different colours and depending what colour the objects have different things will happen.袋子里面有两种不同颜色的物体,根据物体的颜色,会发生不同的事情。 The way I am taking random values right now is:我现在采用随机值的方式是:

while (bag.size() > 1) {
            count++;
            int select1 = rnd.nextInt(bag.size());
            Balls indexPos1 = bag.get(select1);
            int select2 = rnd.nextInt(bag.size());
            Balls indexPos2 = bag.get(select2);
            System.out.println(select1);
            System.out.println(select2);

But at the end of the program if it happens to take an index of the same object then the colour variation inside the bag is wrong and wrong outcome prints.但是在程序结束时,如果它碰巧取了同一个对象的索引,那么袋子内的颜色变化是错误的,并且打印出错误的结果。 I'll paste in the rest of the loop.我将粘贴在循环的其余部分。

int count = 0;
        Random rnd = new Random();
// Depending on what colour the balls are either black ball is placed or white.
        while (bag.size() > 1) {
            count++;
            int select1 = rnd.nextInt(bag.size());
            Balls indexPos1 = bag.get(select1);
            int select2 = rnd.nextInt(bag.size());
            Balls indexPos2 = bag.get(select2);
            System.out.println(select1);
            System.out.println(select2);
            if (bag.size() != 1) {
                System.out.println(bag);
                System.out.println("Printing size: " + bag.size());
                if (bag.get(select1).equals(blackBalls) && indexPos2.equals(blackBalls)) {
                    System.out.println("Both balls are black.");
                    System.out.println("Removing black ball");
                    bag.add(blackBalls);
                    bag.remove(blackBalls);
                    bag.remove(blackBalls);
                    continue;
                }if (indexPos1.equals(whiteBalls) && indexPos2.equals(whiteBalls)) {
                    System.out.println("both Balls are white");
                    bag.add(blackBalls);
                    bag.remove(whiteBalls);
                    bag.remove(whiteBalls);
                    System.out.println("Removing 2 white balls. adding black.");
                    continue;
                }if (indexPos1.equals(whiteBalls) && indexPos2.equals(blackBalls) || indexPos1.equals(blackBalls) && indexPos2.equals(whiteBalls)) {
                    bag.add(whiteBalls);
                    bag.remove(select1);
                    bag.remove(select2);
                    System.out.println("Both balls are different.");
                    System.out.println("Removing white ball");
                }
            }
        }

I have no clue how else to take two random objects from an array, so it doesn't choose the same object twice.我不知道如何从数组中取出两个随机对象,所以它不会两次选择同一个对象。 Any help or feedback is appreciated.任何帮助或反馈表示赞赏。

You have a few options.你有几个选择。

  1. Shuffle the entire collection, then take the first 2 elements.打乱整个集合,然后取前 2 个元素。

    This would of course 'destroy' any order it already has, and it also a bit of a heavy cost to pay, given that you only need 2 random elements, but Collections.shuffle isn't going to be a performance issue unless you have tens of thousands of elements in there.这当然会“破坏”它已有的任何订单,而且支付的成本也有点高,因为您只需要 2 个随机元素,但Collections.shuffle不会成为性能问题,除非您有数以万计的元素在那里。

  2. Keep picking random numbers.继续选择随机数。

    Turn your second 'get a random index' call into a do/while loop, which keeps picking a number until you get a number that isn't equal to the first element.将您的第二个“获取随机索引”调用转换为 do/while 循环,它会不断选择一个数字,直到您获得一个不等于第一个元素的数字。 Note that this will loop forever if you somehow end up with a bag with only 1 element in it, so ensure you check for that first and act appropriately.请注意,如果您以某种方式最终得到一个只有 1 个元素的包,这将永远循环,因此请确保首先检查并采取适当的行动。

  3. Pick a random number 1 less, then adjust.选择一个少 1 的随机数,然后进行调整。

    Instead of .nextInt(bag.size()) , do .nextInt(bag.size() - 1) , and if the index so obtained is equal to, or larger than your first index, increment by 1. This then effectively translates into picking a uniform-distributed random number from the collection [0, 1, 2, 3, 5, 6, 7], assuming a bag of 8 elements, and the first index chosen was 4.而不是.nextInt(bag.size()) ,做.nextInt(bag.size() - 1) ,如果这样获得的索引等于或大于你的第一个索引,增加 1。然后这有效地转换从集合 [0, 1, 2, 3, 5, 6, 7] 中挑选一个均匀分布的随机数,假设一袋有 8 个元素,并且选择的第一个索引是 4。

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

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