Take n random elements from a List?

后端 未结 12 1608
天涯浪人
天涯浪人 2020-11-27 16:09

How can I take n random elements from an ArrayList? Ideally, I\'d like to be able to make successive calls to the take() method to get an

12条回答
  •  感动是毒
    2020-11-27 16:28

    Most of the proposed solutions till now suggest either a full list shuffle or successive random picking by checking uniqueness and retry if required.

    But, we can take advantage of the Durstenfeld's algorithm (the most popular Fisher-Yates variant in our days).

    Durstenfeld's solution is to move the "struck" numbers to the end of the list by swapping them with the last unstruck number at each iteration.

    Due to the above, we don't need to shuffle the whole list, but run the loop for as many steps as the number of elements required to return. The algorithm ensures that the last N elements at the end of the list are 100% random if we used a perfect random function.

    Among the many real-world scenarios where we need to pick a predetermined (max) amount of random elements from arrays/lists, this optimized method is very useful for various card games, such as Texas Poker, where you a-priori know the number of cards to be used per game; only a limited number of cards is usually required from the deck.

    public static  List pickNRandomElements(List list, int n, Random r) {
        int length = list.size();
    
        if (length < n) return null;
    
        //We don't need to shuffle the whole list
        for (int i = length - 1; i >= length - n; --i)
        {
            Collections.swap(list, i , r.nextInt(i + 1));
        }
        return list.subList(length - n, length);
    }
    
    public static  List pickNRandomElements(List list, int n) {
        return pickNRandomElements(list, n, ThreadLocalRandom.current());
    }
    

提交回复
热议问题