Luis solution is fine,
For even more customization, consider looking up on how to write a custom Sampler.