簡體   English   中英

Swift中的隨機數

[英]Random Numbers in Swift

我想創建隨機數,以便在應用程序正面的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])

使用隨機播放

我想你只需要從ballArray中獲得5個不同的隨機池球名稱。 所以你不需要生成任何隨機數。 只需在buttonPressed從shuffled 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為這個隨機數組創建常量並設置圖像,而不在ballArray中的任何地方保存任何帶有硬編碼50個名字的圖像名稱

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

創建一個數組。 它們允許存儲大量數據,您可以參考所有這些數據。 圖像相同

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]
    }
}

生成隨機數並將其插入Set中。 當Set count達到5時,打破循環。 這樣你就可以避免重復。 然后從set中創建隨機數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM