rewrite generateRandom()

cleaner code for generateRandom()