Algorithm to select a single, random combination of values?

后端 未结 7 935
傲寒
傲寒 2020-11-22 14:04

Say I have y distinct values and I want to select x of them at random. What\'s an efficient algorithm for doing this? I could just call rand(

相关标签:
7条回答
  • 2020-11-22 15:03

    Assuming that you want the order to be random too (or don't mind it being random), I would just use a truncated Fisher-Yates shuffle. Start the shuffle algorithm, but stop once you have selected the first x values, instead of "randomly selecting" all y of them.

    Fisher-Yates works as follows:

    • select an element at random, and swap it with the element at the end of the array.
    • Recurse (or more likely iterate) on the remainder of the array, excluding the last element.

    Steps after the first do not modify the last element of the array. Steps after the first two don't affect the last two elements. Steps after the first x don't affect the last x elements. So at that point you can stop - the top of the array contains uniformly randomly selected data. The bottom of the array contains somewhat randomized elements, but the permutation you get of them is not uniformly distributed.

    Of course this means you've trashed the input array - if this means you'd need to take a copy of it before starting, and x is small compared with y, then copying the whole array is not very efficient. Do note though that if all you're going to use it for in future is further selections, then the fact that it's in somewhat-random order doesn't matter, you can just use it again. If you're doing the selection multiple times, therefore, you may be able to do only one copy at the start, and amortise the cost.

    0 讨论(0)
提交回复
热议问题