# Generating user ids on a random forum website

Will this code be random enough to generate user ids for a website where people can create posts? Would there be a better method other than running this function and validating it doesn't exist in the database? I would prefer to have a method that I don't need to run a database check but the resulting UID should be 8 characters at most.

/**
* Creates a UID
* @return string 64 Character UID
*/
public static function createUid($length) {$bytes = random_bytes($length / 2);$uid = bin2hex($bytes); return$uid;
}

• As far as I understand it, the only demands you have is that the UID should be unique and no more than 8 characters. Please define what you mean by 'secure'? – KIKO Software Nov 26 '17 at 20:28
• @KIKOSoftware by secure I️ mean basically never will repeat. I️m not storing sensitive info. – Joe Scotto Nov 28 '17 at 22:54
• That is normally not meant by 'secure', it's called 'unique'. They are two very different concepts. – KIKO Software Nov 29 '17 at 0:42
• php.net/manual/ru/function.uniqid.php#94959 – Your Common Sense Nov 29 '17 at 10:13