Improve error handling in rand_init function