Added lume.weightedchoice()

This commit is contained in:
rxi
2014-03-12 13:12:45 +00:00
parent af4c919be4
commit 2d7fa98155
2 changed files with 26 additions and 0 deletions

View File

@@ -64,6 +64,17 @@ raised.
lume.randomchoice({true, false}) -- Returns either true or false
```
### lume.weightedchoice(t)
Takes the argument table `t` where the keys are the possible choices and the
values are the choice's weight. A weight should be 0 or above, the larger the
weight value the higher the probability of that choice being picked. If the
table is empty or all the weights are 0 or a weight is below zero an error is
raised.
```lua
lume.weightedchoice({ ["cat"] = 10, ["dog"] = 5, ["frog"] = 0 })
-- Returns either "cat" or "dog" with "cat" being twice as likely to be chosen.
```
### lume.shuffle(t)
Shuffles the values of array `t` in place, returns the array.