From c8e5b36060ab25554c56b21699d5de186567b1ac Mon Sep 17 00:00:00 2001 From: qydysky Date: Mon, 31 Aug 2020 07:09:20 +0800 Subject: [PATCH] 53 --- Random.go | 36 ++++++++++++++++++++++++++++++++++++ Sys.go | 7 +++++++ 2 files changed, 43 insertions(+) create mode 100644 Random.go diff --git a/Random.go b/Random.go new file mode 100644 index 0000000..07d1124 --- /dev/null +++ b/Random.go @@ -0,0 +1,36 @@ +package part + +import ( + goRand "math/rand" + goCRand "crypto/rand" + "math/big" + "time" +) + +type random struct{ + RV []interface{} +} + +func Rand() *random { + return &random{} +} + +func (*random) TrueRandom(max int64) int64 { + var e error + if r,e := goCRand.Int(goCRand.Reader, big.NewInt(max)); e == nil { + return r.Int64() + } + Logf().E(e.Error()) + return -1 +} + +func (*random) FakeRandom(max int64) int64 { + r := goRand.New(goRand.NewSource(time.Now().UnixNano())) + return r.Int63n(max) +} + +func (t *random) MixRandom(max int64) int64 { + r := t.TrueRandom(max) + if r != -1 {return r} + return t.FakeRandom(max) +} \ No newline at end of file diff --git a/Sys.go b/Sys.go index 13388e0..1266125 100644 --- a/Sys.go +++ b/Sys.go @@ -61,6 +61,13 @@ func (this *sys) Timeoutf(Timeout int) { time.Sleep(time.Duration(Timeout)*time.Second) } +func (this *sys) MTimeoutf(Millisecond int) { + this.Lock() + defer this.Unlock() + + time.Sleep(time.Duration(Millisecond)*time.Millisecond) +} + func (this *sys) GetSys(sys string)bool{ this.RV = append(this.RV, runtime.GOOS) return runtime.GOOS==sys -- 2.39.2