forward sampling code