
Weighted.pudu
Pudu27 lines895 B
1/** @Chaos.Weighted.Module — picks one of several values by weight */2module PuduLangResilience.Chaos.Weighted34import PuduLangResilience.Chaos as Chaos5import PuduLangResilience.Randomizer as Randomizer6789export fn choose[V](choices: &Array[(Int, V)]) -> fn(Chaos.Arguments) -> Option[V] {10 let options = *choices11 fn(given: Chaos.Arguments) -> Option[V] {12 var total = 013 for choice in options {14 if choice[0] > 0 { total = total + choice[0] }15 }16 if total == 0 { return None }17 var point = Randomizer.below(&given.randomizer, total)18 for choice in options {19 if choice[0] > 0 {20 if point < choice[0] { return Some(choice[1]) }21 point = point - choice[0]22 }23 }24 None25 }26}27