many lint fixes
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -10,16 +10,19 @@ type Rand struct {
|
||||
buf [8]byte
|
||||
}
|
||||
|
||||
// Int31 function implementation
|
||||
func (r *Rand) Int31() int32 {
|
||||
_, _ = crand.Read(r.buf[:4])
|
||||
return int32(binary.BigEndian.Uint32(r.buf[:4]) & ^uint32(1<<31))
|
||||
}
|
||||
|
||||
// Int function implementation
|
||||
func (r *Rand) Int() int {
|
||||
u := uint(r.Int63())
|
||||
return int(u << 1 >> 1) // clear sign bit if int == int32
|
||||
}
|
||||
|
||||
// Float64 function implementation
|
||||
func (r *Rand) Float64() float64 {
|
||||
again:
|
||||
f := float64(r.Int63()) / (1 << 63)
|
||||
@@ -29,6 +32,7 @@ again:
|
||||
return f
|
||||
}
|
||||
|
||||
// Float32 function implementation
|
||||
func (r *Rand) Float32() float32 {
|
||||
again:
|
||||
f := float32(r.Float64())
|
||||
@@ -38,14 +42,17 @@ again:
|
||||
return f
|
||||
}
|
||||
|
||||
// Uint32 function implementation
|
||||
func (r *Rand) Uint32() uint32 {
|
||||
return uint32(r.Int63() >> 31)
|
||||
}
|
||||
|
||||
// Uint64 function implementation
|
||||
func (r *Rand) Uint64() uint64 {
|
||||
return uint64(r.Int63())>>31 | uint64(r.Int63())<<32
|
||||
}
|
||||
|
||||
// Intn function implementation
|
||||
func (r *Rand) Intn(n int) int {
|
||||
if n <= 1<<31-1 {
|
||||
return int(r.Int31n(int32(n)))
|
||||
@@ -53,12 +60,13 @@ func (r *Rand) Intn(n int) int {
|
||||
return int(r.Int63n(int64(n)))
|
||||
}
|
||||
|
||||
// Int63 function implementation
|
||||
func (r *Rand) Int63() int64 {
|
||||
_, _ = crand.Read(r.buf[:])
|
||||
return int64(binary.BigEndian.Uint64(r.buf[:]) & ^uint64(1<<63))
|
||||
}
|
||||
|
||||
// Int31n copied from the standard library math/rand implementation of Int31n
|
||||
// Int31n function implementation copied from the standard library math/rand implementation of Int31n
|
||||
func (r *Rand) Int31n(n int32) int32 {
|
||||
if n&(n-1) == 0 { // n is power of two, can mask
|
||||
return r.Int31() & (n - 1)
|
||||
@@ -71,7 +79,7 @@ func (r *Rand) Int31n(n int32) int32 {
|
||||
return v % n
|
||||
}
|
||||
|
||||
// Int63n copied from the standard library math/rand implementation of Int63n
|
||||
// Int63n function implementation copied from the standard library math/rand implementation of Int63n
|
||||
func (r *Rand) Int63n(n int64) int64 {
|
||||
if n&(n-1) == 0 { // n is power of two, can mask
|
||||
return r.Int63() & (n - 1)
|
||||
@@ -84,7 +92,7 @@ func (r *Rand) Int63n(n int64) int64 {
|
||||
return v % n
|
||||
}
|
||||
|
||||
// Shuffle copied from the standard library math/rand implementation of Shuffle
|
||||
// Shuffle function implementation copied from the standard library math/rand implementation of Shuffle
|
||||
func (r *Rand) Shuffle(n int, swap func(i, j int)) {
|
||||
if n < 0 {
|
||||
panic("invalid argument to Shuffle")
|
||||
|
Reference in New Issue
Block a user