improved randomizing logic