Randomizing collections: one-liners

Monday, August 11, 2008

I found a cool way HERE to shuffle/reorder array elements without using the random class. Generally you provide Sort-Object an object property to sort on, only now the property is in the form of a Globally Globally Unique Identifier (GUID), which is a random generated 128-bit integer.   PS > 1..5 | sort-object { ::newGuid() } 2 5 3 1 4   To select one or more random objects from the collection, pipe the above to Select-Object and use the -First (or -Last) parameter:PS > 1..5 | sort-object { ::newGuid() } | select-object -first 3 4 1 3   These techniques are also available on collections you use everyday with...