Skip to content

takuoki/weightedrand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

weightedrand

GoDoc CI codecov MIT License

A golang package for weighted random.

Usage

items := []weightedrand.WeightedItem[string]{
  {Weight: 5, Item: "Hi!"},
  {Weight: 3, Item: "Hello!"},
  {Weight: 2, Item: "What's up?"},
}

wr := weightedrand.New(time.Now().UnixNano(), items)

fmt.Println(wr.GetItem()) // "Hi!", "Hello!" or "What's up?"
Weight Item Expected Rate
5 "Hi!" 50 % (= 5 / (5 + 3 + 2))
3 "Hello!" 30 % (= 3 / (5 + 3 + 2))
2 "What's up?" 20 % (= 2 / (5 + 3 + 2))

About

A golang package for weighted random.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages