GenerateRandomInts generates a slice of random integers, with each integer ranging from [0, max). The returned slice will be sorted from smallest to largest. If count <= 0 or max <= 0, then they will be set to 1. If count >= max, then the returned slice will contain all incrementing integers [0, max
(count int64, max *big.Int)
| 33 | // will be sorted from smallest to largest. If count <= 0 or max <= 0, then they will be set to 1. If count >= max, then |
| 34 | // the returned slice will contain all incrementing integers [0, max). |
| 35 | func GenerateRandomInts(count int64, max *big.Int) (randInts []*big.Int, err error) { |
| 36 | if count <= 0 { |
| 37 | count = 1 |
| 38 | } |
| 39 | if max.Cmp(BigIntZero) == -1 { |
| 40 | max = BigIntOne |
| 41 | } |
| 42 | // If count >= max, then we'll just shortcut and add incrementing integers up to the max (not including the max) |
| 43 | if big.NewInt(count).Cmp(max) >= 0 { |
| 44 | max64 := max.Int64() |
| 45 | randInts = make([]*big.Int, max64) |
| 46 | for i := int64(0); i < max64; i++ { |
| 47 | randInts[i] = big.NewInt(i) |
| 48 | } |
| 49 | return randInts, nil |
| 50 | } |
| 51 | |
| 52 | randInts = make([]*big.Int, count) |
| 53 | randIntSet := make(map[string]struct{}, count*2) |
| 54 | for i := range randInts { |
| 55 | for { |
| 56 | randInts[i], err = rand.Int(rand.Reader, max) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | if _, ok := randIntSet[randInts[i].String()]; !ok { |
| 61 | randIntSet[randInts[i].String()] = struct{}{} |
| 62 | break |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | sort.Slice(randInts, func(i, j int) bool { |
| 67 | return randInts[i].Cmp(randInts[j]) == -1 |
| 68 | }) |
| 69 | return randInts, nil |
| 70 | } |
| 71 | |
| 72 | // GetPercentages converts the slice of numbers to percentages. The max defines the number that would equal 100%. All |
| 73 | // floats will be between [0.0, 100.0], unless the number is not between [0, max]. |
no test coverage detected