Implemented random sampling.