refactored random number generator