September 29th, 2013, 3:17 pm
This seems like it should be very easy, but my mind is temporarily drawing a blank...I have some code that in its basic form chooses m (unique) items out of a collection of n items, following the algorithm described in pseudocode below.This algorithm seems very inefficient though, particularly when n is large and/or m is close to n. how could it be done better?FUNCTION CHOOSE(n, m) For i =1 To m TryAgain: Generate random integer x between 1 and n If x Not in AnswerCollection Then AnswerCollection.add x Else GOTO TryAgain End if Next i RETURN AnswerCollectionEND FUNCTION