PickRandom returns a random index for a set bit in the bit array. If there is no such value, it returns 0, false. It uses math/rand's global randomness Source to get this index.
()
| 247 | // If there is no such value, it returns 0, false. |
| 248 | // It uses math/rand's global randomness Source to get this index. |
| 249 | func (bA *BitArray) PickRandom() (int, bool) { |
| 250 | if bA == nil { |
| 251 | return 0, false |
| 252 | } |
| 253 | |
| 254 | bA.mtx.Lock() |
| 255 | trueIndices := bA.getTrueIndices() |
| 256 | bA.mtx.Unlock() |
| 257 | |
| 258 | if len(trueIndices) == 0 { // no bits set to true |
| 259 | return 0, false |
| 260 | } |
| 261 | // nolint:gosec // G404: Use of weak random number generator |
| 262 | return trueIndices[mrand.Intn(len(trueIndices))], true |
| 263 | } |
| 264 | |
| 265 | func (bA *BitArray) getTrueIndices() []int { |
| 266 | trueIndices := make([]int, 0, bA.Bits) |