-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastrand.go
More file actions
48 lines (41 loc) · 822 Bytes
/
fastrand.go
File metadata and controls
48 lines (41 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package random
import (
"math/rand"
"sync"
"time"
_ "unsafe"
)
// Implement Source and Source64 interfaces
type rngSource struct {
p sync.Pool
}
func (r *rngSource) Int63() (n int64) {
src := r.p.Get()
n = src.(rand.Source).Int63()
r.p.Put(src)
return
}
// Seed specify seed when using NewRand()
func (r *rngSource) Seed(_ int64) {}
func (r *rngSource) Uint64() (n uint64) {
src := r.p.Get()
n = src.(rand.Source64).Uint64()
r.p.Put(src)
return
}
// NewRand goroutine-safe rand.Rand, optional seed value
func NewRand(seed ...int64) *rand.Rand {
n := time.Now().UnixNano()
if len(seed) > 0 {
n = seed[0]
}
src := &rngSource{
p: sync.Pool{
New: func() interface{} {
return rand.NewSource(n)
},
}}
return rand.New(src)
}
//go:linkname FastRand runtime.fastrand
func FastRand() uint32