Fix weighted random
authorTheSaminator <TheSaminator@users.noreply.github.com>
Thu, 16 Jun 2022 17:34:35 +0000 (13:34 -0400)
committerTheSaminator <TheSaminator@users.noreply.github.com>
Thu, 16 Jun 2022 17:34:35 +0000 (13:34 -0400)
src/commonMain/kotlin/net/starshipfights/game/ai/util.kt

index 6d01dd4d5547570d5ecbab43aac1284d15279be7..1dac8847b1ab54eea0b9a594716331ed9b182e56 100644 (file)
@@ -27,6 +27,7 @@ fun <T : Any> Map<T, Double>.weightedRandomOrNull(random: Random = Random): T? {
        if (values.none { it >= EPSILON }) return null
        
        val total = values.sum()
+       if (total < EPSILON) return null
        
        var hasChoice = false
        var choice = random.nextDouble(total)