-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcg_atomic.go
40 lines (36 loc) · 857 Bytes
/
pcg_atomic.go
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
package fastrand
import (
"sync/atomic"
)
// AtomicPCG implements the PCG-XSH-RR generator with atomic state updates.
//
// This generator is safe for concurrent use by multiple goroutines.
// The zero value is a valid state: Seed() can be called to set a custom seed.
type AtomicPCG struct {
state atomic.Uint64
}
// Seed initializes the state with the provided seed.
//
// This function is safe for concurrent use by multiple goroutines.
func (r *AtomicPCG) Seed(s uint64) {
var t PCG
t.Seed(s)
r.state.Store(t.state)
}
// Uint32 returns a random uint32.
//
// This function is safe for concurrent use by multiple goroutines.
func (r *AtomicPCG) Uint32() uint32 {
i := uint32(0)
for {
old := r.state.Load()
var t PCG
t.state = old
n := t.Uint32()
if r.state.CompareAndSwap(old, t.state) {
return n
}
i += 30
cpuYield(i)
}
}