nico-s
6/7/2017 - 7:32 PM

ViewController.swift

func computeMonteCarlo(iterations: Int) {

    DispatchQueue.concurrentPerform(iterations: iterations) { _ in

        let (x, y) = (drand48() * 2 - 1, drand48() * 2 - 1)
        let point = UIView(frame: CGRect(x: 3 * (x + 1) * 50, y: 3 * (y + 1) * 50, width: 3, height: 3))

        if x * x + y * y <= 1 {
            nPointsInside += 1
            point.backgroundColor = UIColor.red
        } else {
            point.backgroundColor = UIColor.blue
        }

        DispatchQueue.main.async {
            self.monteCarloGraph.addSubview(point)
        }
    }
}