dimanche 5 mai 2019

How to shuffle an array of N size without any imports

The goal is to shuffle an array of N size. No imports can be used within the code. i.e. Random

I am able to do it with ease using imports as shown below although these are forbidden.

private static void shuffleArray(int[] array)
  {
      int index, temp;
      Random random = new Random();
      for (int i = array.length - 1; i > 0; i--)
      {
          index = random.nextInt(i + 1);
          temp = array[index];
          array[index] = array[i];
          array[i] = temp;
      }
  }

End goal is to shuffle the array in a random order. Pseudo-random is perfectly fine.




Aucun commentaire:

Enregistrer un commentaire