Random number without repetition

I have an array with 4 strings. These correspond to 4 images. One should be selected at random from this array.

I have a userScore that is set over 👍 and 👎 button, up and down. Each time a random number is generated via Int.random (in: 0 .. <4), which then also ensures that the corresponding view is reloaded and a new image is displayed.

Now the array only consists of 4 elements and therefore it is often repeated.

How can I best solve this, that it is excluded that the previous number is selected again? Does anyone have a tip for me?

You could add the four indexes (0 to 3) to an array, in random order.
Then:

  • removeLast()
  • Use this index with your string array
  • Repeat, until the index array is empty

Then (if necessary) start again, with a new random order

Random number without repetition
 
 
Q