title: Swift random number
body: <p>I'm having problems with this drawRandomCard function. </p>
  
  <p>It works just like it should for some time, but eventually it crashes the application. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>import Foundation
  
  
  var cardDeck = Array&lt;PlayingCard&gt;()
  
  class Deck {
  
      func addCard(card : PlayingCard , atTop : Bool = false){
  
          if atTop {
              cardDeck.insert(card, atIndex: 0);
          }else{
              cardDeck += card
          }
      }
  
      func drawRandomCard() -&gt; PlayingCard{
          var card = PlayingCard()
          var randomNumber : Int = Int(arc4random()) % (cardDeck.count - 1)
          card = cardDeck[randomNumber]
          cardDeck.removeAtIndex(randomNumber)
          return card
      }
  
  }
  </code></pre>