Skip to content

Instantly share code, notes, and snippets.

@Megaprog
Created March 18, 2016 09:29
Show Gist options
  • Save Megaprog/b0e173bcaba5f54fc918 to your computer and use it in GitHub Desktop.
Save Megaprog/b0e173bcaba5f54fc918 to your computer and use it in GitHub Desktop.
class EnoughParticipantsMonteCarlo extends EnoughParticipants {
override def calculate(probabilities: List[Double], needed: Int): Double = {
(1 to attempts).par.map(i => probabilities.map(p => dice(p)).sum).count(successful => successful >= needed) / attempts.toDouble
}
val Precision = 0.001
val attempts = Math.ceil(Math.pow(1 / Precision, 2)).toInt
def dice(probability: Double) = if (ThreadLocalRandom.current().nextDouble() < probability) 1 else 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment