MCPcopy Create free account
hub / github.com/tendermint/tendermint / PickRandom

Method PickRandom

libs/bits/bit_array.go:249–263  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

247// If there is no such value, it returns 0, false.
248// It uses math/rand's global randomness Source to get this index.
249func (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
265func (bA *BitArray) getTrueIndices() []int {
266 trueIndices := make([]int, 0, bA.Bits)

Callers 4

TestPickRandomFunction · 0.95
gossipDataForCatchupMethod · 0.80
gossipDataRoutineMethod · 0.80
PickVoteToSendMethod · 0.80

Calls 3

getTrueIndicesMethod · 0.95
LockMethod · 0.65
UnlockMethod · 0.65

Tested by 1

TestPickRandomFunction · 0.76