Open matfournier opened 2 years ago
package dice
import java.util.Random
class Dice {
val random = new Random()
def rollD6: Int =
random.nextInt(6) + 1
}
object NCOStats extends App {
val dice = new Dice()
def roll(runs: Int, nPositive: Int, nNegative: Int, threshold: Int = 4): Double = {
val successes = Range(0, runs).inclusive.toList.map {_ =>
val posRolls = List.fill(nPositive)("").map(_ => (dice.rollD6, 1)).groupBy(_._1).view.mapValues(v => v.map(_._2).sum)
val negRolls = List.fill(nNegative)("").map(_ => (dice.rollD6, 1)).groupBy(_._1).view.mapValues(v => v.map(_._2).sum)
val result = posRolls.toList.map {
case (dice, count) =>
(dice, negRolls.get(dice).map(sub => count - sub).getOrElse(count))
}.filter {
case (_, count) => count > 0
}
if (result.isEmpty) 0 else {
if (result.maxBy(_._1)._1 >= threshold) 1 else 0
}
}
successes.count(i => i == 1).toDouble / runs.toDouble
}
case class Scenario(nPositive: Int, nNegative: Int) {
def id = s"$nPositive vs $nNegative"
}
val scenarios = for {
positive <- List(1,2,3,4,5,6)
negative <- List(0,1,2,3,4,5,6)
} yield Scenario(positive, negative)
val success = 6
val partialOrBetter = 4
val results = scenarios.map(scenario => (scenario, roll(runs = 100000, scenario.nPositive, scenario.nNegative, partialOrBetter)))
// same results as here
// https://github.com/HAnthonyHoyt/neon-city-overdrive-dice/blob/main/RAND_RESULTS.md
// looking at partial success or better FYI
results.foreach {
case (res, result) =>
val truncated = f"$result%1.2f"
println(s"${res.id}: $truncated")
}
}
I'm glad to see some conformation then from another source. What language is this by the way? It looks like Java but it seems to lack semicolons. Is this Groovy?
What language is this by the way? It looks like Java but it seems to lack semicolons. Is this Groovy?
It's Scala. 😁
Couldn't reply to your locked reddit thread, just wanted to point out I get the same results as you based on a quick script I whipped up over lunch, not whatever the OG poster was posting.
For partial or better:
for just success (6):