improved implementation of Multinomial sampling