package seqs

import (
	cr "crypto/rand"
	"math/big"
	mr "math/rand"
	"time"
)

// 随机生成器;
type rand struct {
}

// 全局随机数;
var rnd *mr.Rand

func init() {
	rnd = mr.New(mr.NewSource(time.Now().UnixNano()))
}

// 构建;
func Rand() *rand {
	return &rand{}
}

// 随机数字;
func (rd *rand) DigitId(_len int) string {
	return rd.newId([]byte("0123456789"), _len)
}

// 随机字母;
func (rd *rand) ChrtId(_len int) string {
	return rd.newId([]byte("abcdefghijklmnopqrstuvwxyz"), _len)
}

// 随机混合(数字+字母);
func (rd *rand) BothId(_len int) string {
	return rd.newId([]byte("0123456789abcdefghijklmnopqrstuvwxyz"), _len)
}

// 随机范围(长整型);
func (rd *rand) RandI64(min, max int64) int64 {
	bi, _ := cr.Int(cr.Reader, big.NewInt(max-min))
	return min + bi.Int64()
}

// 随机范围(0 ~ max);
func (rd *rand) RandInt(max int) int {
	return rnd.Intn(max)
}

// 随机中文;
func (rd *rand) ChrtCn(_len int) string {
	a := make([]rune, _len)
	for i := range a {
		a[i] = rune(rd.RandI64(19968, 40869))
	}
	return string(a)
}

// newId;
func (rd *rand) newId(tmpl []byte, _len int) string {
	var r []byte
	for i := 0; i < _len; i++ {
		r = append(r, tmpl[rnd.Intn(len(tmpl))])
	}
	return string(r)
}
