Skip to content

Commit

Permalink
fix random integer generator division by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Feb 19, 2025
1 parent bd8ba8b commit 66b0745
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,34 @@ func Coins() sdk.Coins {
// Uint64InRange returns a sample uint64 in the given ranges
func Uint64InRange(low, high uint64) uint64 {
r := newRandFromSeed(int64(low))
if low == high {
return low // avoid division by zero
}
return r.Uint64()%(high-low) + low
}

// Uint64InRange returns a sample uint64 in the given ranges
func Uint64InRangeFromRand(r *rand.Rand, low, high uint64) uint64 {
if low == high {
return low // avoid division by zero
}
return r.Uint64()%(high-low) + low
}

// Int64InRange returns a sample int64 in the given ranges
func Int64InRange(low, high int64) int64 {
r := newRandFromSeed(low)
if low == high {
return low // avoid division by zero
}
return r.Int63()%(high-low) + low
}

// Int64InRangeFromRand returns a sample int64 in the given ranges
func Int64InRangeFromRand(r *rand.Rand, low, high int64) int64 {
if low == high {
return low // avoid division by zero
}
return r.Int63()%(high-low) + low
}

Expand Down

0 comments on commit 66b0745

Please sign in to comment.