Skip to content

Instantly share code, notes, and snippets.

@Kaiochao
Created January 18, 2015 00:30
Show Gist options
  • Save Kaiochao/e080feb73f4870b9beaa to your computer and use it in GitHub Desktop.
Save Kaiochao/e080feb73f4870b9beaa to your computer and use it in GitHub Desktop.
Pseudo-randomly pick an item from a list using its associated weight.
// 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