[英]Having Trouble with Random Numbers in Swift
我正在嘗試生成隨機的背景色,以在用戶按下屏幕上的按鈕時顯示給用戶。 我有一個不可變的UIColor項數組,並且已經制作了一個可變副本來進行操作。 當生成隨機顏色時,然后將該顏色返回並從數組的可變副本中刪除,以防止連續顯示相同顏色,直到顯示所有顏色為止。 這應該一直發生到數組的計數為0,然后重新創建數組以重復該過程。 但是,當我深入到2到0之間的數組時,循環似乎變成了無限循環。 我的代碼(操場文件)中缺少什么邏輯?
var currentColorIndexNumber = 0
var newColorIndexNumber = 0
let colorsArray = [
UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), //teal color
UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), //yellow color
UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), //red color
UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), //orange color
UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), //dark color
UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), //purple color
UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), //green color
]
var mutableColorsArray: [AnyObject] = colorsArray
func randomNumber() -> Int {
// avoid repeating random integers
while currentColorIndexNumber == newColorIndexNumber {
var unsignedArrayCount = UInt32(mutableColorsArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
newColorIndexNumber = Int(unsignedRandomNumber)
}
currentColorIndexNumber = newColorIndexNumber
return newColorIndexNumber
}
func randomColor() -> UIColor {
var randomIndex = randomNumber()
var randomColor = mutableColorsArray[randomIndex] as UIColor
mutableColorsArray.removeAtIndex(randomIndex)
if mutableColorsArray.count == 0 {
mutableColorsArray = colorsArray
}
return randomColor
}
嘗試改組。 遵循以下原則:
var colorsArray = ["red","green","blue","yellow","purple"]
var currentColorIndexNumber = colorsArray.count
func shuffle<T>(inout array: Array<T>) -> Void {
for index in 0..<colorsArray.count {
let swapLocation = index + Int(arc4random_uniform(UInt32(colorsArray.count - index)))
if index != swapLocation {
swap(&array[swapLocation], &array[index])
}
}
}
func randomColor() -> String {
// shuffle first time through, then every time the list is exhausted
if currentColorIndexNumber >= colorsArray.count - 1 {
// shuffle, but guarantee first color after shuffling
// is not the same as last color from previous round
do {
shuffle(&colorsArray)
} while colorsArray[0] == lastColor
currentColorIndexNumber = -1
lastColor = colorsArray[colorsArray.count-1]
}
currentColorIndexNumber += 1
return colorsArray[currentColorIndexNumber]
}
for i in (5*colorsArray.count) {
print("\(randomColor()) ")
if i % colorsArray.count == 0 {
println()
}
}
我的成績很好
func randomObject(array: [AnyObject]) -> AnyObject {
return array[Int(arc4random_uniform(UInt32(array.count)))]
}
要以隨機順序枚舉所有元素,只需使用下面的shuffle函數,按順序使用shuffled數組的元素,並在數組用盡后重新隨機排列。
func shuffleArray<T>(array: Array<T>) -> Array<T> {
for var i = array.count - 1; i > 0; i-- {
var j = Int(arc4random_uniform(UInt32(i-1)))
swap(&array[i], &array[j])
}
return array
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.