2014-09-24 91 views
0

我試圖在屏幕上按下按鈕時生成隨機背景顏色以顯示給用戶。我有一個UIColor項目的不可變數組,我做了一個可變副本來操作。當生成隨機顏色時,該顏色將被返回並從陣列的可變副本中移除,以防止連續顯示相同的顏色,直到顯示所有顏色。這應該發生,直到數組的計數爲0,然後重新創建數組重複該過程。但是,當我回到2到0項之間的數組時,循環似乎變成了一個無限循環。我在代碼(操場文件)中缺少什麼邏輯?在Swift中遇到隨機數問題

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 
} 

回答

0

嘗試改用洗牌。沿着這些路線的東西:

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() 
    } 
} 
0

我有良好的結果

func randomObject(array: [AnyObject]) -> AnyObject { 
    return array[Int(arc4random_uniform(UInt32(array.count)))] 
} 

要列舉按隨機順序元素的所有,只需使用隨機播放功能,如下一個,使用元素然後重新洗牌數組,一旦數組耗盡,重新洗牌。

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 
} 
+0

這不會枚舉所有的顏色而不會重複,我讀爲bnkohrn的意圖。 – pjs 2014-09-24 18:00:56

+0

沒錯,我忽略了這個要求。 – Mundi 2014-09-24 22:16:45