improved implementation of Beta sampling