Use Fisher-Yates shuffle (Wikipedia)
You can easily follow this guideline to create your own Java implementation:
- Pick a random integer
r
from the range[0..array.length)
- Swap
array[0]
andarray[r]
- Pick a new random integer
r
from the range[1..array.length)
- Swap
array[1]
andarray[r]
and so on. This translates fairly easily to a loop. The performance is good, there's no need to reallocate the array or create new objects and execution time scales linearly.