Skip to content

Instantly share code, notes, and snippets.

@JohannMG
Created June 2, 2017 21:47
Show Gist options
  • Select an option

  • Save JohannMG/16da823da748ab642196d6d1ad637d8a to your computer and use it in GitHub Desktop.

Select an option

Save JohannMG/16da823da748ab642196d6d1ad637d8a to your computer and use it in GitHub Desktop.
Dice that hit a 5 or 6 can be re-rolled to doubled-down. Three dice are rolled. What's the the probability of getting 2 or more total points.
//: Playground - noun: a place where people can play
import Cocoa
/*
Problem: There are three dice.
It counts as a point if any die hits a 5 or 6
Any die that gets a 5 ot 6 is rolled again. If the second roll is 5 ot 6 that die has produced two points.
*/
struct Die {
let value: Int = Die.roll()
private static func roll() -> Int {
var totalValue = 0
let rollA = arc4random_uniform(6)
if rollA == 4 || rollA == 5 {
totalValue += 1
} else {
return 0
}
let rollB = arc4random_uniform(6)
if rollB == 4 || rollB == 5 {
totalValue += 1
}
return totalValue
}
}
var counter = [0,0,0 ,0, 0,0,0]
( 0..<100_000 ).forEach { _ in
let newDieTotalValue = Die().value + Die().value + Die().value
counter[newDieTotalValue] = counter[newDieTotalValue] + 1
}
print(counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment