corrected rand function