-1
我有这样的代码:六个随机数字,但重复?怎么不重复?斯威夫特
import UIKit
class ViewController: UIViewController {
@IBAction func geraNumeros(_ sender: Any) {
var numeroAleatorio:[Int] = []
for _ in 1...6 {
numeroAleatorio.append(Int(arc4random_uniform(60)+1))
}
numeroAleatorio.sort()
labelNumeros.text = "\(numeroAleatorio[0]) - \(numeroAleatorio[1]) - \(numeroAleatorio[2]) - \(numeroAleatorio[3]) - \(numeroAleatorio[4]) - \(numeroAleatorio[5]) "
}
@IBOutlet weak var labelNumeros: UILabel!
}
但有时号码重复,如:
10 - 18 - 12 - 10 - 30 - 60
10-10一样,所以做什么?
有时随机产生的数字重复。考虑你扔掷骰子的时间;你不能保证每次都有不同的号码。 –
https://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift可能是你在找什么。 –
或者:https://stackoverflow.com/questions/27541145/how-to-generate-a-random-number-in-swift-without-repeating-the-previous-random-n。 –