我试图这样做,当我点击一个按钮时,它会生成1到13之间的随机int(包括),它不会连续两次给出相同的数字。 相当新的快速btw
我已经在这里查看了许多其他主题,仍然无法使其发挥作用。
random int function:
func randomIntBetween(low:Int, high:Int) -> Int {
let range = high - (low - 1)
return (Int(arc4random()) % range) + (low - 1)
}
和按钮:
@IBAction func higher(sender: AnyObject) {
//Random Number between 1-13
numberLabel.text = String(randomIntBetween(2, high: 14))
print(numberLabel.text)
由于
答案 0 :(得分:0)
创建一个存储最后使用的Int的变量,如果匹配新的随机,则调用该函数。
var lastUsedRandom: Int = 0
func randomIntBetween(low:Int, high:Int) -> Int {
let range = high - (low - 1)
let newRandom = (Int(arc4random()) % range) + (low - 1)
if newRandom == lastUsedRandom{
randomIntBetween(low, high: high)
}else{
lastUsedRandom = newRandom
return newRandom
}
}