Shuffle Algorithm (Swap Method)
The shuffle algorithm is a way to generate random numbers that never repeat. For example, suppose you need to generate ten random numbers, and all ten must be “unique.” Many people’s first instinct is to call the rand function to get a random value, store it in an array, and compare it against the numbers already in the array. That method is simple, but its performance is very poor. The shuffle algorithm introduced here is worlds apart from that approach in terms of performance. The principle behind the shuffle algorithm is actually very simple: first store the numbers in an array, then scramble them. ...