You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nrandom/int.go

35 lines
637 B
Go

1 year ago
package nrandom
import (
"math/rand"
"time"
)
// RandInt generate random int between min and max, maybe min, not be max.
//
// Usage:
//
// RandInt(10, 99)
// RandInt(100, 999)
// RandInt(1000, 9999)
func RandInt(min, max int) int {
return RandIntWithSeed(min, max, time.Now().UnixNano())
}
// RandIntWithSeed return a random int at the [min, max)
// Usage:
//
// seed := time.Now().UnixNano()
// RandomIntWithSeed(1000, 9999, seed)
func RandIntWithSeed(min, max int, seed int64) int {
if min == max {
return min
}
if max < min {
min, max = max, min
}
r := rand.New(rand.NewSource(seed))
return r.Intn(max-min) + min
}