Verify and correct rand and rand_key with unit tests