2023-03-05 23:35:41 +07:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2023-03-13 09:24:59 +07:00
|
|
|
"fmt"
|
2023-03-05 23:35:41 +07:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-03-06 15:15:11 +07:00
|
|
|
|
|
|
|
"github.com/shopspring/decimal"
|
2023-03-05 23:35:41 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
// RandomFloat with rounding for testing
|
2023-03-12 11:01:43 +07:00
|
|
|
func RandomFloat(min, max float64) float64 {
|
2023-03-06 15:15:11 +07:00
|
|
|
val := (min + rand.Float64()*(max-min)) * 100
|
2023-03-12 11:01:43 +07:00
|
|
|
val, _ = decimal.NewFromFloat(val).Truncate(2).Float64()
|
|
|
|
return val
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
2023-03-13 09:24:59 +07:00
|
|
|
|
|
|
|
func RandomEmail() string {
|
|
|
|
return fmt.Sprintf("%s@mail.com", RandomString(5))
|
|
|
|
}
|
2023-03-19 18:17:17 +07:00
|
|
|
|
|
|
|
func RandomTransactionCode(prefix string, merchant_index int64) string {
|
|
|
|
time_now := time.Now().Unix()
|
|
|
|
return fmt.Sprintf("%s%d%d", prefix, merchant_index, time_now)
|
|
|
|
}
|