| 1 | package wyhash |
| 2 | |
| 3 | import "sync" |
| 4 | |
| 5 | // global is a parallel rng for the package functions. |
| 6 | var global struct { |
| 7 | sync.Mutex |
| 8 | RNG |
| 9 | } |
| 10 | |
| 11 | // Int returns a random int. |
| 12 | // Safe for concurrent callers. |
| 13 | func Int() int { |
| 14 | global.Lock() |
| 15 | out := global.Int() |
| 16 | global.Unlock() |
| 17 | return out |
| 18 | } |
| 19 | |
| 20 | // Intn returns a int uniformly in [0, n). |
| 21 | // Safe for concurrent callers. |
| 22 | func Intn(n int) int { |
| 23 | global.Lock() |
| 24 | out := global.Intn(n) |
| 25 | global.Unlock() |
| 26 | return out |
| 27 | } |
| 28 | |
| 29 | // Uint64 returns a random uint64. |
| 30 | // Safe for concurrent callers. |
| 31 | func Uint64() uint64 { |
| 32 | global.Lock() |
| 33 | out := global.Uint64() |
| 34 | global.Unlock() |
| 35 | return out |
| 36 | } |
| 37 | |
| 38 | // Uint64n returns a uint64 uniformly in [0, n). |
| 39 | // Safe for concurrent callers. |
| 40 | func Uint64n(n uint64) uint64 { |
| 41 | global.Lock() |
| 42 | out := global.Uint64n(n) |
| 43 | global.Unlock() |
| 44 | return out |
| 45 | } |
| 46 | |
| 47 | // Float64 returns a float64 uniformly in [0, 1). |
| 48 | // Safe for concurrent callers. |
| 49 | func Float64() float64 { |
| 50 | global.Lock() |
| 51 | out := global.Float64() |
| 52 | global.Unlock() |
| 53 | return out |
| 54 | } |