swift - 有人可以帮我阻止 arc4random 生成的数字再次出现吗?

标签 swift if-statement arc4random

我正在使用 arc4random 生成一个随机数,然后我用它来显示图像 Assets ,但我需要一个按钮,该按钮将在按下 arc4random 后再次生成该数字

 @IBAction func Button(_ sender: Any) {


        let BugRandom = arc4random_uniform(18)


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


    }

有谁知道这是否可行或我应该寻找什么?

最佳答案

试试这个:

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)")
}

但是有警告,18次后明显失效。

关于swift - 有人可以帮我阻止 arc4random 生成的数字再次出现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198404/

相关文章:

r - 如何通过替换 "for-loop"和 "if-else"子句来提高大型数据集的性能

python - 条件 If 语句 : If value in row contains string . .. 设置另一列等于字符串

ios - 如何根据单词生成随机字母

ios - Swift iOS - 在文本字段内输入时按下 uiview

swift - TextView 自定义子类 - textViewDidChange 不触发

ios - 快速更改 UITabBarItem 图像和色调颜色

ios - secondVC 中的结构数组未从 firstVC 附加

linux - bash 脚本每天运行但不是每个月的第一天

Swift 在 0 和 1 之间随机 float

iOS 编程 : Making an arc4_random not repeat itself using a do while loop