title: Having Trouble with Random Numbers in Swift
body: <p>I am trying to generate random background colors to display to a user when they press a button on the screen. I have an immutable array of UIColor items and I have made a mutable copy to manipulate. When a random color is generated, that color is then returned and removed from the mutable copy of the array to prevent consecutive showings of the same color until all colors have been shown. This is supposed to happen until the array's count is 0, and then it recreates the array to repeat the process. However, when I get down to the array having between 2 and 0 items, the loop seems to turn into an infinite loop. What logic am I missing in my code (playground file)?</p>
  
  <pre><code>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() -&gt; 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() -&gt; UIColor {
      var randomIndex = randomNumber()
      var randomColor = mutableColorsArray[randomIndex] as UIColor
      mutableColorsArray.removeAtIndex(randomIndex)
      if mutableColorsArray.count == 0 {
          mutableColorsArray = colorsArray
      }
      return randomColor
  }
  </code></pre>