base code for sampling