Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Monte-Carlo-methods/Swift/monte-carlo-methods.swift
Normal file
26
Task/Monte-Carlo-methods/Swift/monte-carlo-methods.swift
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import Foundation
|
||||
|
||||
func mcpi(sampleSize size:Int) -> Double {
|
||||
var x = 0 as Double
|
||||
var y = 0 as Double
|
||||
var m = 0 as Double
|
||||
|
||||
for i in 0..<size {
|
||||
x = Double(arc4random()) / Double(UINT32_MAX)
|
||||
y = Double(arc4random()) / Double(UINT32_MAX)
|
||||
|
||||
if ((x * x) + (y * y) < 1) {
|
||||
m += 1
|
||||
}
|
||||
}
|
||||
|
||||
return (4.0 * m) / Double(size)
|
||||
}
|
||||
|
||||
println(mcpi(sampleSize: 100))
|
||||
println(mcpi(sampleSize: 1000))
|
||||
println(mcpi(sampleSize: 10000))
|
||||
println(mcpi(sampleSize: 100000))
|
||||
println(mcpi(sampleSize: 1000000))
|
||||
println(mcpi(sampleSize: 10000000))
|
||||
println(mcpi(sampleSize: 100000000))
|
||||
Loading…
Add table
Add a link
Reference in a new issue