Fixed implementation of top and randRange