moved random number code out of function