close to the edge
Member
What's the best method for removing a random element from a list? I have a list of Scrabble letters and I want to remove a random element of the list and return it. (the list being the bag of letters and removing a letter would be like drawing a letter to put it on your rack) More specifically, are the following two equivalent:
Take a random number from [0, n[ and use it as an index to the list
Shuffle the list (Fisher-Yates shuffle for instance) and return the first element.
The first one is obviously more efficient, but I'm not sure if it's also correct.
Take a random number from [0, n[ and use it as an index to the list
Shuffle the list (Fisher-Yates shuffle for instance) and return the first element.
The first one is obviously more efficient, but I'm not sure if it's also correct.