Skip to content

Commit da12165

Browse files
committed
Add altered random number generation
1 parent 0611d2e commit da12165

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

x/mock/simulation/rand_util.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package simulation
22

33
import (
4+
"fmt"
45
"math/big"
56
"math/rand"
67
"time"
@@ -37,14 +38,37 @@ func RandStringOfLength(r *rand.Rand, n int) string {
3738
}
3839

3940
// Generate a random amount
41+
// Note: The range of RandomAmount includes max, and is, in fact, biased to return max.
4042
func RandomAmount(r *rand.Rand, max sdk.Int) sdk.Int {
41-
return sdk.NewInt(int64(r.Intn(int(max.Int64()))))
43+
// return sdk.NewInt(int64(r.Intn(int(max.Int64()))))
44+
max2 := big.NewInt(0).Mul(max.BigInt(), big.NewInt(2))
45+
randInt := big.NewInt(0).Rand(r, max2)
46+
if randInt.Cmp(max.BigInt()) > 0 {
47+
randInt = max.BigInt()
48+
}
49+
result := sdk.NewIntFromBigInt(randInt)
50+
// Sanity
51+
if result.GT(max) {
52+
panic(fmt.Sprintf("%v > %v", result, max))
53+
}
54+
return result
4255
}
4356

4457
// RandomDecAmount generates a random decimal amount
58+
// Note: The range of RandomDecAmount includes max, and is, in fact, biased to return max.
4559
func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec {
46-
randInt := big.NewInt(0).Rand(r, max.Int)
47-
return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision)
60+
// randInt := big.NewInt(0).Rand(r, max.Int)
61+
max2 := big.NewInt(0).Mul(max.Int, big.NewInt(2))
62+
randInt := big.NewInt(0).Rand(r, max2)
63+
if randInt.Cmp(max.Int) > 0 {
64+
randInt = max.Int
65+
}
66+
result := sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision)
67+
// Sanity
68+
if result.GT(max) {
69+
panic(fmt.Sprintf("%v > %v", result, max))
70+
}
71+
return result
4872
}
4973

5074
// RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts

0 commit comments

Comments
 (0)