Fix a bug with function _random_str()

Ensure function does not generate illegal UTF-8 characters.