Skip to content

Commit 0d64275

Browse files
authored
Merge pull request #6 from zifter/fix-corner-case-with-1-choose
Fix case with 1 choice which have weight equals 1
2 parents 953da99 + 432cb4b commit 0d64275

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

weightedrand.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewChooser(choices ...Choice) (*Chooser, error) {
5252
totals[i] = runningTotal
5353
}
5454

55-
if runningTotal <= 1 {
55+
if runningTotal < 1 {
5656
return nil, errNoValidChoices
5757
}
5858

weightedrand_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func TestNewChooser(t *testing.T) {
5858
cs: []Choice{{Item: 'a', Weight: 0}, {Item: 'b', Weight: 0}},
5959
wantErr: errNoValidChoices,
6060
},
61+
{
62+
name: "choice with weight equals 1",
63+
cs: []Choice{{Item: 'a', Weight: 1}},
64+
wantErr: nil,
65+
},
6166
{
6267
name: "weight overflow",
6368
cs: []Choice{{Item: 'a', Weight: maxInt/2 + 1}, {Item: 'b', Weight: maxInt/2 + 1}},

0 commit comments

Comments
 (0)