I have a Set
which contains multiple Elements
. My goal is to get 10 random Elements
out of it.
How can I achieve this goal in the simplest way (hopefully one line) without having to modify the Set
itself or create a new reference Set
?
Here's the shortest one-liner I came up with:
let randomTenElements = mySet.lazy.shuffled().prefix(10)
It returns an ArraySlice
though so if you need the result as an array or another set you'll have to explicitly cast it to your desired type.