Improve random generator function