Write random selection algorithm