Optimized sampling code