refactored code
refactored random logic into functions