building randomselection algorithm