mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 13:07:36 +03:00
43 lines
690 B
Go
43 lines
690 B
Go
package number
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"math/big"
|
|
|
|
"golang.org/x/exp/constraints"
|
|
)
|
|
|
|
// TODO Remove on Go 1.22, in favor of builtin `min` function.
|
|
func Min[T constraints.Ordered](vs ...T) T {
|
|
if len(vs) == 0 {
|
|
var zero T
|
|
return zero
|
|
}
|
|
min := vs[0]
|
|
for _, v := range vs[1:] {
|
|
if v < min {
|
|
min = v
|
|
}
|
|
}
|
|
return min
|
|
}
|
|
|
|
// TODO Remove on Go 1.22, in favor of builtin `max` function.
|
|
func Max[T constraints.Ordered](vs ...T) T {
|
|
if len(vs) == 0 {
|
|
var zero T
|
|
return zero
|
|
}
|
|
max := vs[0]
|
|
for _, v := range vs[1:] {
|
|
if v > max {
|
|
max = v
|
|
}
|
|
}
|
|
return max
|
|
}
|
|
|
|
func RandomInt64(max int64) int64 {
|
|
rnd, _ := rand.Int(rand.Reader, big.NewInt(max))
|
|
return rnd.Int64()
|
|
}
|