go.x2ox.com/sorbifolia/random@v0.0.0-20240520090142-6d8be5c4ed59/math.go (about) 1 package random 2 3 import ( 4 "math/rand/v2" 5 ) 6 7 type mathRand struct{ *randString } 8 9 func Math() Random { 10 return &mathRand{defaultRandString} 11 } 12 13 func (r mathRand) RandString(length int) string { 14 arr := make([]int, length) 15 for i := range arr { 16 arr[i] = int(uint64(rand.Uint32()) * uint64(r.randBytesLen) >> 32) 17 } 18 return r.randString.RandString(arr) 19 } 20 21 func (r mathRand) SetRandBytes(data []byte) Random { 22 return &mathRand{r.randString.SetRandBytes(data)} 23 } 24 25 func (r mathRand) Uint() uint { return uint(rand.Uint64()) } 26 func (r mathRand) Uint64() uint64 { return rand.Uint64() } 27 func (r mathRand) Uint32() uint32 { return rand.Uint32() } 28 func (r mathRand) Uint16() uint16 { return uint16(rand.Uint32()) } 29 func (r mathRand) Uint8() uint8 { return uint8(rand.Uint32()) } 30 31 func (r mathRand) Int() int { return toPtr[int](r.Uint()) } 32 func (r mathRand) Int64() int64 { return toPtr[int64](r.Uint64()) } 33 func (r mathRand) Int32() int32 { return toPtr[int32](r.Uint32()) } 34 func (r mathRand) Int16() int16 { return toPtr[int16](r.Uint16()) } 35 func (r mathRand) Int8() int8 { return toPtr[int8](r.Uint8()) } 36 37 func (r mathRand) Uintn(n uint) uint { return remainder(n, r.Uint()) } 38 func (r mathRand) Uint64n(n uint64) uint64 { return remainder(n, r.Uint64()) } 39 func (r mathRand) Uint32n(n uint32) uint32 { return remainder(n, r.Uint32()) } 40 func (r mathRand) Uint16n(n uint16) uint16 { return remainder(n, r.Uint16()) } 41 func (r mathRand) Uint8n(n uint8) uint8 { return remainder(n, r.Uint8()) } 42 43 func (r mathRand) Intn(n int) int { return remainder(n, r.Int()) } 44 func (r mathRand) Int64n(n int64) int64 { return remainder(n, r.Int64()) } 45 func (r mathRand) Int32n(n int32) int32 { return remainder(n, r.Int32()) } 46 func (r mathRand) Int16n(n int16) int16 { return remainder(n, r.Int16()) } 47 func (r mathRand) Int8n(n int8) int8 { return remainder(n, r.Int8()) }