mirror of
https://github.com/TangentFoxy/lume.git
synced 2024-11-19 07:04:24 +00:00
Added lume.weightedchoice()
This commit is contained in:
parent
af4c919be4
commit
2d7fa98155
11
README.md
11
README.md
@ -64,6 +64,17 @@ raised.
|
|||||||
lume.randomchoice({true, false}) -- Returns either true or false
|
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)
|
### lume.shuffle(t)
|
||||||
Shuffles the values of array `t` in place, returns the array.
|
Shuffles the values of array `t` in place, returns the array.
|
||||||
|
|
||||||
|
15
lume.lua
15
lume.lua
@ -67,6 +67,21 @@ function lume.randomchoice(t)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.weightedchoice(t)
|
||||||
|
local sum = 0
|
||||||
|
for k, v in pairs(t) do
|
||||||
|
assert(v >= 0, "weight value less than zero")
|
||||||
|
sum = sum + v
|
||||||
|
end
|
||||||
|
assert(sum ~= 0, "all weights are zero")
|
||||||
|
local rnd = lume.random(sum)
|
||||||
|
for k, v in pairs(t) do
|
||||||
|
if rnd < v then return k end
|
||||||
|
rnd = rnd - v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.shuffle(t)
|
function lume.shuffle(t)
|
||||||
for i = 1, #t do
|
for i = 1, #t do
|
||||||
local r = math.random(#t)
|
local r = math.random(#t)
|
||||||
|
Loading…
Reference in New Issue
Block a user