the sampling algorithm