A Go package for weighted random option selection
package main
import (
"fmt"
"log"
"github.com/eljamo/weightedoption/v2"
)
// Simulates 100 chances for dropping a raid exotic weapon from Destiny, which has a 5% drop chance when a player completes the raid
func main() {
// Create a new selector with options and their weights
s, err := weightedoption.NewSelector(
weightedoption.NewOption('🔫', 5), // 5% chance for the exotic weapon
weightedoption.NewOption('❌', 95), // 95% chance for no drop
)
if err != nil {
log.Fatal(err)
}
chances := make([]rune, 100) // Array to store the results of 100 attempts
for i := 0; i < len(chances); i++ {
chances[i] = s.Select() // Select an option based on their weights
}
fmt.Println(string(chances))
tally := make(map[rune]int)
for _, c := range chances {
tally[c]++
}
_, err = fmt.Printf("\n🔫: %d\t❌ %d\n", tally['🔫'], tally['❌'])
if err != nil {
log.Fatal(err)
}
}
go run ./example/example.go
❌❌❌❌❌❌❌❌❌❌❌❌🔫❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌🔫❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌❌🔫❌❌❌❌❌❌❌❌❌❌❌❌
🔫: 3 ❌ 97
If you'd like to contribute, please fork the repository and work your magic. Open a pull request to the main
branch if it is a bugfix
or feature
branch. If it is a hotfix
branch, open a pull request to the respective release
branch.
go test --race --shuffle on ./...
go run ./example/example.go
bugfix/*
feature/*
main
release/v*
hotfix/*
-
feature
andbugfix
branches:feature
Branches: Create these for new features you are developing.bugfix
Branches: Create these for fixing bugs identified in themain
branch.- Once the feature or fix is complete, merge these branches back into the
main
branch.
-
main
branch:- The
main
branch serves as the central integration branch where allfeature
andbugfix
branches are merged.
- The
-
release
branch:- When you are ready to make a release, create a
release
branch from themain
branch. - Perform any final testing and adjustments on the
release
branch. - Once the release is stable, it can be deployed from this branch.
- When you are ready to make a release, create a
-
hotfix
branch:- If a critical issue is found after the release, create a
hotfix
branch from therelease
branch. - Fix the issue on the
hotfix
branch and then merge it back into both therelease
andmain
branches if applicable. - This ensures that the fix is included in the current release(s) and the
main
branch.
- If a critical issue is found after the release, create a
-
Developing a feature:
- Create a
feature
branch frommain
. - Develop and test the feature on the
feature
branch. - Merge the
feature
branch intomain
.
- Create a
-
Fixing a bug:
- Create a
bugfix
branch frommain
. - Fix and test the bug on the
bugfix
branch. - Merge the
bugfix
branch intomain
.
- Create a
-
Making a release:
- Create a
release
branch frommain
or anotherrelease
branch. - Perform testing on
release
branch. - Deploy the
release
branch.
- Create a
-
Applying a hotfix:
- Create a
hotfix
branch from arelease
branch. - Fix the critical issue on
hotfix
branch. - Merge
hotfix
branch into therelease
andmain
branches.
- Create a