mercredi 15 mars 2017

How can I generate a random number without repeating the previous number in swift 3

I know that this question has been asked before, but the answers I found doesn't seem to help. I have an array of a structure, and I want to keep generating random numbers to access the elements of that array without repeating any of the previous elements until it completely cycles through the array. After which, I would like it to start again.

This is what I have done so far.

var previousNumber: Int = 0

func generateRandumNumbers() -> Int {

    var randomNumber: Int = Int(arc4random_uniform(UInt32(wordArray.count)))

    while previousNumber == randomNumber {
        randomNumber = Int(arc4random_uniform(UInt32(wordArray.count)))
    }

    previousNumber = randomNumber

    return randomNumber
}

Thank you. Any kind of help will be appreciated.




Aucun commentaire:

Enregistrer un commentaire