sample.by helper function