Created
June 28, 2018 17:19
-
-
Save jyrkive/08807b2e4c8b8660c2935e9e7161b647 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function weighted_random(odds) | |
local target = wesnoth.random() | |
local sum = 0.0 | |
local i = 1 | |
sum += odds[1] | |
while sum <= target and i <= #odds do | |
sum += odds[i] | |
i += 1 | |
end | |
return i | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment