title: Using Grand Central Dispatch in Swift to parallelize and speed up “for" loops?
body: <p>I am trying to wrap my head around how to use GCD to parallelize and speed up Monte Carlo simulations. Most/all simple examples are presented for Objective C and I really need a simple example for Swift since Swift is my first “real” programming language.</p>
  
  <p>The minimal working version of a monte carlo simulation in Swift would be something like this:</p>
  
  <pre><code>import Foundation
  
  import Cocoa
  var winner = 0
  var j = 0
  var i = 0
  var chance = 0
  var points = 0
  for j=1;j&lt;1000001;++j{
      var ability = 500
  
      var player1points = 0
  
      for i=1;i&lt;1000;++i{
          chance = Int(arc4random_uniform(1001))
          if chance&lt;(ability-points) {++points}
          else{points = points - 1}
      }
      if points &gt; 0{++winner}
  }
      println(winner)
  </code></pre>
  
  <p>The code works directly pasted into a command line program project in xcode 6.1</p>
  
  <p>The innermost loop cannot be parallelized because the new value of variable “points” is used in the next loop. But the outermost just run the innermost simulation 1000000 times and tally up the results and should be an ideal candidate for parallelization.</p>
  
  <p>So my question is how to use GCD to parallelize the outermost for loop?</p>