Skip to content

Instantly share code, notes, and snippets.

@hmhmsh
Created May 22, 2020 08:50
Show Gist options
  • Save hmhmsh/1b699fbb626f83aff64afc647a3045e3 to your computer and use it in GitHub Desktop.
Save hmhmsh/1b699fbb626f83aff64afc647a3045e3 to your computer and use it in GitHub Desktop.
input: 確率 output: 当たりかどうか
func hit(percent: Float) -> Bool {
let max = Int(100 / fabsf(percent))
let value = Int.random(in: 1 ... max)
return value == 1
}
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