(cases []element[T])
| 16 | } |
| 17 | |
| 18 | func oneOf[T any](cases []element[T]) T { |
| 19 | total := 0 |
| 20 | for _, c := range cases { |
| 21 | total += c.weight |
| 22 | } |
| 23 | r := rand.Intn(total) |
| 24 | for _, c := range cases { |
| 25 | if r < c.weight { |
| 26 | return c.value |
| 27 | } |
| 28 | r -= c.weight |
| 29 | } |
| 30 | return cases[0].value |
| 31 | } |
| 32 | |
| 33 | func random[T any](array []T) T { |
| 34 | return array[rand.Intn(len(array))] |
no outgoing calls
no test coverage detected
searching dependent graphs…