Used the implementation of the Alias method for sampling
(http://en.wikipedia.org/wiki/Alias_method) from @jcazevedo.