How to get long 19 digits of random numbers in PHP?
Therefore, I need to make a unique mobile game purchase number. Each user who buys a position record in the application must be bound with unique long 19 digit numbers from the php code and stored in the mysql DB.
But how can I actually make the random 19 digits?
I tried like this but errors.
$num_str = sprintf("%19d", mt_rand(1, 9999999999999999999));
+3
source to share
3 answers
You should do something like:
$num_str = sprintf("%10d", mt_rand(1, mt_getrandmax())).sprintf("%9d", mt_rand(1, mt_getrandmax()));
if (strlen($num_str) < 19)
$num_str = str_pad($num_str, 19, rand(0, 9), STR_PAD_RIGHT);
else if(strlen($num_str) > 19)
$num_str = substr($num_str, 0, 19);
echo $num_str;
The distribution of mt_rand () return values โโshifts towards even numbers in 64-bit PHP builds when max exceeds 2 ^ 32. This is because if max is greater than the value returned by mt_getrandmax (), the output of the random number generator must be incremented.
0
source to share