forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monte_carlo.swift
46 lines (34 loc) · 1.18 KB
/
monte_carlo.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Double Extension from YannickSteph on StackOverflow: https://stackoverflow.com/questions/25050309/swift-random-float-between-0-and-1
import Foundation
public extension Double {
public static var random: Double {
return Double(arc4random()) / 0xFFFFFFFF // Returns a random double between 0.0 and 1.0, inclusive.
}
public static func random(min: Double, max: Double) -> Double {
return Double.random * (max - min) + min
}
}
func isInCircle(x: Double, y: Double, radius: Double) -> Bool {
return (x*x) + (y*y) < radius*radius
}
func monteCarlo(n: Int) -> Double {
let radius: Double = 1
var piCount = 0
var randX: Double
var randY: Double
for _ in 0...n {
randX = Double.random(min: 0, max: radius)
randY = Double.random(min: 0, max: radius)
if(isInCircle(x: randX, y: randY, radius: radius)) {
piCount += 1
}
}
let piEstimate = Double(4 * piCount)/(Double(n))
return piEstimate
}
func main() {
let piEstimate = monteCarlo(n: 10000)
print("Pi estimate is: ", piEstimate)
print("Percent error is: \(100 * abs(piEstimate - Double.pi)/Double.pi)%")
}
main()