-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
311 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package gameserver | ||
|
||
import ( | ||
"errors" | ||
"math/rand" | ||
"sort" | ||
) | ||
|
||
type WeightedRand struct { | ||
cumulativeWeights []int64 | ||
values []uint32 | ||
totalWeight int64 | ||
} | ||
|
||
func NewWeightedRand() *WeightedRand { | ||
return &WeightedRand{} | ||
} | ||
|
||
func (w *WeightedRand) Add(value uint32, weight int64) error { | ||
// Detect if the total weight is going to overflow | ||
if w.totalWeight+weight < w.totalWeight { | ||
return errors.New("total weight will overflow") | ||
} | ||
|
||
// Update total weight | ||
w.totalWeight += weight | ||
|
||
// Append the value | ||
w.values = append(w.values, value) | ||
|
||
// Compute cumulative weight | ||
var cumulativeWeight int64 | ||
if len(w.cumulativeWeights) > 0 { | ||
cumulativeWeight = w.cumulativeWeights[len(w.cumulativeWeights)-1] + weight | ||
} else { | ||
cumulativeWeight = weight | ||
} | ||
w.cumulativeWeights = append(w.cumulativeWeights, cumulativeWeight) | ||
|
||
return nil | ||
} | ||
|
||
func (w *WeightedRand) Choose() uint32 { | ||
// Generate a random number in the range [0, totalWeight) | ||
r := rand.Int63n(w.totalWeight) | ||
|
||
// Use binary search to find the index where our random number fits in | ||
index := sort.Search(len(w.cumulativeWeights), func(i int) bool { return w.cumulativeWeights[i] > r }) | ||
|
||
// Return the corresponding value | ||
return w.values[index] | ||
} |
Oops, something went wrong.