better random algorithm