Skip to content

Commit

Permalink
performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
alparslanahmed committed Feb 16, 2023
1 parent 1316fec commit 0e34a74
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions weightedRandom.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ func WeightedRandom[P interface{ GetWeight() int64 }](elements []P) (P, error) {
total.Add(total, big.NewInt(element.GetWeight()))
}

var shuffler []P
var rng, _ = rand.Int(rand.Reader, total)
var randomNumber = rng.Int64()
var countWeight int64 = 0
var target P

for _, element := range elements {
for j := int64(0); j < element.GetWeight(); j++ {
shuffler = append(shuffler, element)
var weight = element.GetWeight()

if weight < 0 {
continue
}

countWeight += weight
if randomNumber < countWeight {
target = element
break
}
}

var rnd, _ = rand.Int(rand.Reader, total)
return shuffler[rnd.Int64()], nil
return target, nil
}

0 comments on commit 0e34a74

Please sign in to comment.