mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
36 lines
487 B
Go
36 lines
487 B
Go
package main
|
|
|
|
import (
|
|
"math/rand"
|
|
)
|
|
|
|
func maybe() bool {
|
|
return rand.Intn(2) == 0
|
|
}
|
|
|
|
type list[T any] []element[T]
|
|
|
|
type element[T any] struct {
|
|
value T
|
|
weight int
|
|
}
|
|
|
|
func oneOf[T any](cases []element[T]) T {
|
|
total := 0
|
|
for _, c := range cases {
|
|
total += c.weight
|
|
}
|
|
r := rand.Intn(total)
|
|
for _, c := range cases {
|
|
if r < c.weight {
|
|
return c.value
|
|
}
|
|
r -= c.weight
|
|
}
|
|
return cases[0].value
|
|
}
|
|
|
|
func random[T any](array []T) T {
|
|
return array[rand.Intn(len(array))]
|
|
}
|