Non repeating random numbers

Fisher-yates shuffle algorithm is the way to go. Its efficient for shuffling.
and it works in linear time.

here is algo

To shuffle an array a of n elements:
  for i from n − 1 downto 1 do
       j ← random integer with 0 ≤ j ≤ i
       exchange a[j] and a[i]

and the code

for(int i=VALUES.length-1; i>0; i--){
            int rand = (int) (Math.random()*i);
            char temp = VALUES[i];
            VALUES[i] = VALUES[rand];
            VALUES[rand] = temp;
    }

Leave a Comment