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

39 lines
538 B

package tencent
import (
"math/rand"
"sync"
"time"
)
var Worker *worker
type worker struct {
sync.Mutex
timestamp int64
number uint32
randNum uint32
}
func init() {
rand.Seed(time.Now().UnixNano())
Worker = &worker{
randNum: rand.Uint32(),
}
}
// 获取1秒内不重复的随机数
func (w *worker) Rander() uint32 {
w.Lock()
defer w.Unlock()
now := time.Now().Unix()
if now == w.timestamp {
w.number++
} else {
w.number = 0
w.timestamp = now
w.randNum = rand.Uint32()
}
return w.randNum ^ w.number
}