dPompa Blog

Computing Weighted Random

Today I stumbled on a nice problem in a project I’m working on. I have an array with objects where each object has a weight. I then need to pick an object randomly while considering the weights (i.e heavier objects will get picked more often). If you think about this problem a bit you’ll definitely come up with a working solution, but Eli Bendersky took the time to sum it up nicely for all of us.

Thanks Eli! :)

Leave a Reply