naise_pos/util/random.go

40 lines
762 B
Go

package util
import (
"math/rand"
"strings"
"time"
"github.com/shopspring/decimal"
)
const alphabet = "abcdefghijklmnopqrstuvwxyz"
func init() {
rand.Seed(time.Now().UnixNano())
}
// RandomInt generates a random integer between min and max
func RandomInt(min, max int64) int64 {
return min + rand.Int63n(max-min+1)
}
// RandomFloat with rounding for testing
func RandomFloat(min, max float64) (float64, bool) {
val := (min + rand.Float64()*(max-min)) * 100
return decimal.NewFromFloat(val).Truncate(2).Float64()
}
// RandomString generates a random string of length n
func RandomString(n int) string {
var sb strings.Builder
k := len(alphabet)
for i := 0; i < n; i++ {
c := alphabet[rand.Intn(k)]
sb.WriteByte(c)
}
return sb.String()
}