Refactored random number generation for const correctness