Created
May 22, 2020 08:50
-
-
Save hmhmsh/1b699fbb626f83aff64afc647a3045e3 to your computer and use it in GitHub Desktop.
input: 確率 output: 当たりかどうか
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func hit(percent: Float) -> Bool { | |
let max = Int(100 / fabsf(percent)) | |
let value = Int.random(in: 1 ... max) | |
return value == 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let percent: Float = 5 // 5% | |
let sum = (0..<10).map { _ -> Int in | |
(0..<100).map { _ in hit(percent: percent) }.filter { $0 }.count | |
}.reduce(0, +) | |
let average: Float = Float(sum) / 10.0 | |
print("平均: \(average)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment