move randomization to sql query