I'll note that I've used Greg McKaskle's last solutions in several simulation and test applications in Java by using something similar to this:
Code:
LinkedList<Object> someList = getList();
Collections.rotate(someList, (int)(Math.random()*(someList.size() - 1)));
Object randomObject = someList.remove();
- Rotating is usually much faster than shuffling
- Removing the element from the queue ensures lack of duplicates
If you want a good lesson in C, you could try implementing a basic generic rotate function and a basic generic LinkedList object. This isn't the most efficient way in Java since the
Collections class doesn't know about the
Queue implementation of the
LinkedList (thus it could save time by manipulating indices rather than doing a rotation), but implementing your own solution would give you that flexibility.