PHP随机数与Guid的生成

try {
    $string = random_bytes(16);
} catch (TypeError $e) {
    // Well, it's an integer, so this IS unexpected.
    die("An unexpected error has occurred");
} catch (Error $e) {
    // This is also unexpected because 32 is a reasonable integer.
    die("An unexpected error has occurred");
} catch (Exception $e) {
    // If you get this message, the CSPRNG failed hard.
    die("Could not generate a random string. Is our OS secure?");
}

var_dump(bin2hex($string));

核心函数random_bytes在PHP7中,较旧的版本可以

composer require paragonie/random_compat

然后就可以调用random_int等更好的随机数函数了

你可能感兴趣的:(PHP随机数与Guid的生成)