github.com/puellanivis/breton@v0.2.16/lib/rand/wrappers.go (about) 1 package rand 2 3 import ( 4 "math/rand" 5 "time" 6 ) 7 8 // Float32 returns Float32 from the global random source. 9 func Float32() float32 { 10 return globalRand.Float32() 11 } 12 13 // Float64 returns Float64 from the global random source. 14 func Float64() float64 { 15 return globalRand.Float64() 16 } 17 18 // ExpFloat64 returns ExpFloat64 from the global random source. 19 func ExpFloat64() float64 { 20 return globalRand.ExpFloat64() 21 } 22 23 // NormFloat64 returns NormFloat64 from the global random source. 24 func NormFloat64() float64 { 25 return globalRand.NormFloat64() 26 } 27 28 // Int returns Int from the global random source. 29 func Int() int { 30 return globalRand.Int() 31 } 32 33 // Intn returns Intn from the global random source. 34 func Intn(n int) int { 35 return globalRand.Intn(n) 36 } 37 38 // Int31 returns Int31 from the global random source. 39 func Int31() int32 { 40 return globalRand.Int31() 41 } 42 43 // Int31n returns Int31n from the global random source. 44 func Int31n(n int32) int32 { 45 return globalRand.Int31n(n) 46 } 47 48 // Int63 returns Int63 from the global random source. 49 func Int63() int64 { 50 return globalRand.Int63() 51 } 52 53 // Int63n returns Int63n from the global random source. 54 func Int63n(n int64) int64 { 55 return globalRand.Int63n(n) 56 } 57 58 // Uint32 returns Uint32 from the global random source. 59 func Uint32() uint32 { 60 return globalRand.Uint32() 61 } 62 63 // Uint64 returns two Uint32 calls arranged into a uint64. 64 func Uint64() uint64 { 65 return uint64(Uint32())<<32 | uint64(Uint32()) 66 } 67 68 // Perm returns Perm from the global random source. 69 func Perm(n int) []int { 70 return globalRand.Perm(n) 71 } 72 73 // Read returns Read from the global random source. 74 func Read(p []byte) (n int, err error) { 75 return globalRand.Read(p) 76 } 77 78 // Rand is a wrapper around the math/rand.Rand object. 79 type Rand struct { 80 *rand.Rand 81 } 82 83 // New returns a new random source in the vein of math/rand.New. 84 func New(src Source) *Rand { 85 return &Rand{ 86 Rand: rand.New(src), 87 } 88 } 89 90 // SecureSeed seeds the random source with a cryptographically secure seed. 91 func (r *Rand) SecureSeed() { 92 r.Seed(genSeed()) 93 } 94 95 // Reseeder sets up a Reseeder on the random source to periodically reseed with a cryptographically secure seed. 96 func (r *Rand) Reseeder(d time.Duration) { 97 r.SecureSeed() 98 99 for range time.Tick(d) { 100 r.SecureSeed() 101 } 102 } 103 104 // Source is an alias for rand.Source. 105 type Source = rand.Source 106 107 // NewSource returns rand.NewSource(seed). 108 func NewSource(seed int64) Source { 109 return rand.NewSource(seed) 110 }