moved the sampling code to mathutil