swiftif-statementarc4random

Can someone help me stop an arc4random generated number from appearing again?


Im using arc4random to generate a random number which im then using to display an image asset but i need a button that will stop arc4random generating that number again once its pressed

 @IBAction func Button(_ sender: Any) {


        let BugRandom = arc4random_uniform(18)


        Smallbug.image = UIImage(named: "Bug\(BugRandom)")


    }

Does anyone know if this is possible or what i should be looking for?


Solution

  • try this:

    var randomAssets: Set<Int> = Set(0...18)
    
    @IBAction func Button(_ sender: Any) {
        guard let random = randomAssets.randomElement() else {
            return
        }
        randomAssets.remove(random)
        Smallbug.image = UIImage(named: "Bug\(random)")
    }
    

    But there is a warning, after 18 times it stop working obviously.