Created
January 18, 2015 00:30
-
-
Save Kaiochao/e080feb73f4870b9beaa to your computer and use it in GitHub Desktop.
Pseudo-randomly pick an item from a list using its associated weight.
This file contains 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
// e.g. stuff = list(A = 25, B = 10) (null gets as 65% chance) | |
proc/pick_weighted(stuff[]) | |
if(args.len > 1) stuff = args.Copy() | |
var random = rand() * 100, a = 0, b, chance | |
for(var/item in stuff) | |
chance = stuff[item] | |
b = a + chance | |
if(random in a to b) return item | |
a = b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment