Refactoring: Extract function to generate random n