在Swift中遇到随机数问题

时间:2014-09-24 15:14:00

标签: swift

我正在尝试生成随机背景颜色,以便在用户按下屏幕上的按钮时显示给用户。我有一个不可变的UIColor项目数组,我已经制作了一个可变副本来操作。当生成随机颜色时,然后返回该颜色并从阵列的可变副本中移除该颜色,以防止在显示所有颜色之前连续显示相同颜色。这应该发生在数组的计数为0之前,然后它重新创建数组以重复该过程。但是,当我到达具有2到0项的数组时,循环似乎变成了无限循环。我的代码(playground文件)中缺少什么逻辑?

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
}

2 个答案:

答案 0 :(得分: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()
  }
}

答案 1 :(得分:0)

我的成绩很好

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
}