What is the best way to randomize items in an array and also remove random items to get to a required array length?
Let’s say I have an array with 100 items.
First, I want to randomize it.
Then, I have three percent coefficients: 10%, 30%, 60%.
I want to remove items from the original array so the new array I get will be 60% of the original size (60 items), then 30%, then 10%.
The best way to randomise the array would be to use a Fisher-Yates-Durstenfeld shuffle. You could either do this in-place (ie, shuffle the existing array) or create a new collection containing the items in random order.
Here’s how I’d probably do it, creating a new shuffled collection rather than mutating the original array, and then using
SkipandTaketo get the required sub-sequences of the shuffled array. (And it would be easy enough to alter this code to perform an in-place shuffle instead, if you prefer.)