|
1 | 1 | package simulation |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "math/big" |
5 | 6 | "math/rand" |
6 | 7 | "time" |
@@ -37,14 +38,37 @@ func RandStringOfLength(r *rand.Rand, n int) string { |
37 | 38 | } |
38 | 39 |
|
39 | 40 | // Generate a random amount |
| 41 | +// Note: The range of RandomAmount includes max, and is, in fact, biased to return max. |
40 | 42 | 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 |
42 | 55 | } |
43 | 56 |
|
44 | 57 | // RandomDecAmount generates a random decimal amount |
| 58 | +// Note: The range of RandomDecAmount includes max, and is, in fact, biased to return max. |
45 | 59 | 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 |
48 | 72 | } |
49 | 73 |
|
50 | 74 | // RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts |
|
0 commit comments