refactored the random code to optimize the Not Beaten searches