lundi 30 décembre 2019

How to create random list without repeating one previous value?

This code creates list of random numbers, but sometimes value of one previous index is repeated. I can use code below to avoid repeating, but it creates random integer, not list. So, I need to avoid list to be like this [1,1,0,5] and want it to be like this [1,0,1,5]

var previousIndex = 0

private fun getNewRandomIndex(): Int {
        var newIndex = -1
        while (true) {
            newIndex = random.nextInt(randomValues.size)
            if (previousIndex != newIndex) {
                previousIndex = newIndex
                break
            }
        }
        return newIndex
    }



Aucun commentaire:

Enregistrer un commentaire