best way to pick a random subset from a collection?

Jon Bentley discusses this in either ‘Programming Pearls’ or ‘More Programming Pearls’. You need to be careful with your N of M selection process, but I think the code shown works correctly. Rather than randomly shuffle all the items, you can do the random shuffle only shuffling the first N positions – which is a useful saving when N << M.

Knuth also discusses these algorithms – I believe that would be Vol 3 “Sorting and Searching”, but my set is packed pending a move of house so I can’t formally check that.

Leave a Comment