Refactor random number generator