Rewrite the function CircularSample and make it more efficient