Random sampling and scoring code.