Browse Source

common/rand: Remove exponential distribution functions (#1979)

We were computing these functions incorrectly.
I'm not sure what distribution these numbers are, but it isn't the
normal exponential distribution. (We're making the probability of
getting a number of a particular bitlength equal, but the number in
that bitlength thats gets chosen is uniformly chosen)

We weren't using these functions anywhere in our codebase, and they
had a nomenclature error. (There aren't exponentially distributed
integers, instead they would be geometrically distributed)
pull/1986/head
Dev Ojha 6 years ago
committed by Anton Kaliaev
parent
commit
71859f8f3b
3 changed files with 5 additions and 50 deletions
  1. +4
    -1
      CHANGELOG.md
  2. +1
    -46
      libs/common/random.go
  3. +0
    -3
      libs/common/random_test.go

+ 4
- 1
CHANGELOG.md View File

@ -1,6 +1,9 @@
# Changelog # Changelog
## PENDING
## TBA
BREAKING CHANGES:
- [libs/common] remove exponentially distributed random numbers
IMPROVEMENTS: IMPROVEMENTS:
- [config] Increase default send/recv rates to 5 mB/s - [config] Increase default send/recv rates to 5 mB/s


+ 1
- 46
libs/common/random.go View File

@ -109,18 +109,6 @@ func RandInt63n(n int64) int64 {
return grand.Int63n(n) return grand.Int63n(n)
} }
func RandUint16Exp() uint16 {
return grand.Uint16Exp()
}
func RandUint32Exp() uint32 {
return grand.Uint32Exp()
}
func RandUint64Exp() uint64 {
return grand.Uint64Exp()
}
func RandFloat32() float32 { func RandFloat32() float32 {
return grand.Float32() return grand.Float32()
} }
@ -247,39 +235,6 @@ func (r *Rand) Int63n(n int64) int64 {
return i63n return i63n
} }
// Distributed pseudo-exponentially to test for various cases
func (r *Rand) Uint16Exp() uint16 {
bits := r.Uint32() % 16
if bits == 0 {
return 0
}
n := uint16(1 << (bits - 1))
n += uint16(r.Int31()) & ((1 << (bits - 1)) - 1)
return n
}
// Distributed pseudo-exponentially to test for various cases
func (r *Rand) Uint32Exp() uint32 {
bits := r.Uint32() % 32
if bits == 0 {
return 0
}
n := uint32(1 << (bits - 1))
n += uint32(r.Int31()) & ((1 << (bits - 1)) - 1)
return n
}
// Distributed pseudo-exponentially to test for various cases
func (r *Rand) Uint64Exp() uint64 {
bits := r.Uint32() % 64
if bits == 0 {
return 0
}
n := uint64(1 << (bits - 1))
n += uint64(r.Int63()) & ((1 << (bits - 1)) - 1)
return n
}
func (r *Rand) Float32() float32 { func (r *Rand) Float32() float32 {
r.Lock() r.Lock()
f32 := r.rand.Float32() f32 := r.rand.Float32()
@ -295,7 +250,7 @@ func (r *Rand) Float64() float64 {
} }
func (r *Rand) Time() time.Time { func (r *Rand) Time() time.Time {
return time.Unix(int64(r.Uint64Exp()), 0)
return time.Unix(int64(r.Uint64()), 0)
} }
// Bytes returns n random bytes generated from the internal // Bytes returns n random bytes generated from the internal


+ 0
- 3
libs/common/random_test.go View File

@ -73,9 +73,6 @@ func testThemAll() string {
fmt.Fprintf(out, "randInt64: %d\n", RandInt64()) fmt.Fprintf(out, "randInt64: %d\n", RandInt64())
fmt.Fprintf(out, "randUint32: %d\n", RandUint32()) fmt.Fprintf(out, "randUint32: %d\n", RandUint32())
fmt.Fprintf(out, "randUint64: %d\n", RandUint64()) fmt.Fprintf(out, "randUint64: %d\n", RandUint64())
fmt.Fprintf(out, "randUint16Exp: %d\n", RandUint16Exp())
fmt.Fprintf(out, "randUint32Exp: %d\n", RandUint32Exp())
fmt.Fprintf(out, "randUint64Exp: %d\n", RandUint64Exp())
return out.String() return out.String()
} }


Loading…
Cancel
Save