我想创建一个随机数,以显示在应用程序正面的5个UIImage框中。但是,我希望这些数字不总是在同一范围内。例如,下面的代码显示randomPoolBallIndex3
的0 ... 9。但是,我希望它显示0到49之间的任何数字,但不要在其他randomPoolBallIndex上重复相同的数字。因此,每按一次该按钮都不会显示,例如1、1、34、35和50,但是每个数字都会不同。
有没有办法做到这一点?
我将每个randomPoolBallIndex的数组从0 ... 49分解,但是现在它们仅显示我设置的范围,虽然解决了复制问题,但我并不完全满意。
下面的代码:
let ballArray = ["poolball1","poolball2","poolball3","poolball4","poolball5","poolball6","poolball7","poolball8","poolball9","poolball10","poolball11","poolball12","poolball13","poolball14","poolball15","poolball16","poolball17","poolball18","poolball19","poolball20","poolball21","poolball22","poolball23","poolball24","poolball25","poolball26","poolball27","poolball28","poolball29","poolball30","poolball31","poolball32","poolball33","poolball34","poolball35","poolball36","poolball37","poolball38","poolball39","poolball40","poolball41","poolball42","poolball43","poolball44","poolball45","poolball46","poolball47","poolball48","poolball49","poolball50"]
var randomPoolBallIndex: Int = 0
var randomPoolBallIndex1: Int = 0
var randomPoolBallIndex2: Int = 0
var randomPoolBallIndex3: Int = 0
var randomPoolBallIndex4: Int = 0
var randomPoolBallIndex5: Int = 0
@IBOutlet weak var poolBallView1: UIImageView!
@IBOutlet weak var poolBallView2: UIImageView!
@IBOutlet weak var poolBallView3: UIImageView!
@IBOutlet weak var poolBallView4: UIImageView!
@IBOutlet weak var poolBallView5: UIImageView!
@IBAction func buttonPressed(_ sender: UIButton) {
randomPoolBallIndex1 = Int.random(in: 20 ... 29)
randomPoolBallIndex2 = Int.random(in: 40 ... 49)
randomPoolBallIndex3 = Int.random(in: 0 ... 9)
randomPoolBallIndex4 = Int.random(in: 30 ... 39)
randomPoolBallIndex5 = Int.random(in: 10 ... 19)
poolBallView1.image = UIImage(named: ballArray[randomPoolBallIndex1])
poolBallView2.image = UIImage(named: ballArray[randomPoolBallIndex2])
poolBallView3.image = UIImage(named: ballArray[randomPoolBallIndex3])
poolBallView4.image = UIImage(named: ballArray[randomPoolBallIndex4])
poolBallView5.image = UIImage(named: ballArray[randomPoolBallIndex5])
答案 0 :(得分:7)
我想您只需要从ballArray中获得5个不同的随机池球名称即可。因此,您无需生成任何随机数。只需在buttonPressed
中,从改组后的ballArray中创建一个常量
let shuffledBallArray = ballArray.shuffled()
现在只需设置如下图像:
poolBallView1.image = UIImage(named: shuffledBallArray[0])
poolBallView2.image = UIImage(named: shuffledBallArray[1])
...
因此,您的buttonPressed
操作应如下所示:
@IBAction func buttonPressed(_ sender: UIButton) {
let shuffledBallArray = ballArray.shuffled()
poolBallView1.image = UIImage(named: shuffledBallArray[0])
poolBallView2.image = UIImage(named: shuffledBallArray[1])
poolBallView3.image = UIImage(named: shuffledBallArray[2])
poolBallView4.image = UIImage(named: shuffledBallArray[3])
poolBallView5.image = UIImage(named: shuffledBallArray[4])
}
或者您可以创建为您提供5个唯一随机数的函数
func generateNumbers(repetitions: Int, maxValue: Int) -> [Int] {
var numbers = [Int]()
for _ in 1...repetitions {
var n: Int
repeat {
n = Int.random(in: 1...maxValue)
} while numbers.contains(n)
numbers.append(n)
}
return numbers
}
在buttonPressed
中,只需为此随机数数组创建常量并设置图像,而无需将任何图像名称保存在具有硬编码的50个名称的ballArray中
@IBAction func buttonPressed(_ sender: UIButton) {
let randomNumbers = generateNumbers(repetitions: 5, maxValue: 50)
poolBallView1.image = UIImage(named: "poolBall\(randomNumbers[0])")
poolBallView2.image = UIImage(named: "poolBall\(randomNumbers[1])")
poolBallView3.image = UIImage(named: "poolBall\(randomNumbers[2])")
poolBallView4.image = UIImage(named: "poolBall\(randomNumbers[3])")
poolBallView5.image = UIImage(named: "poolBall\(randomNumbers[4])")
}
答案 1 :(得分:0)
创建一个数组。它们允许存储大量数据,您可以引用所有数据。图片相同
var randomPoolBallIndices:[Int]!
@IBOutlet weak var poolBallViews: [UIImageView]! //Look up how to make array from IBOutlets
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
randomPoolBallIndices = Array(repeating: 0, count: 5)
}
@IBAction func buttonPressed(_ sender: UIButton) {
for index in randomPoolBallIndices.indices {
let number = Int.random(in: 0 ... 49)
while (randomPoolBallIndices.contains(number)) {
number = Int.random(in: 0 ... 49)
}
randomPoolBallIndices[index] = number
poolBallViews[index] = randomPoolBallIndices[index]
}
}
答案 2 :(得分:0)
生成随机数并将其插入Set中。当设置计数达到5时,中断循环。这样,您可以避免重复。然后从集合中创建随机数数组。