Home > Article > Backend Development > Why is the result of Alibaba Cloud server’s random number generation fixed?
The random number generation result is always the same on Alibaba Cloud
No problem locally
<code>public function randStr($len = 6, $format = 'ALL') { switch ($format) { case 'ALL': $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~'; break; case 'CHAR': $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~'; break; case 'NUMBER': $chars = '0123456789'; break; default : $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~'; break; } mt_srand((double) microtime() * 1000000 * getmypid()); $password = ""; while (strlen($password) < $len) { $password.=substr($chars, (mt_rand() % strlen($chars)), 1); } return $password; }</code>
The random number generation result is always the same on Alibaba Cloud
No problem locally
<code>public function randStr($len = 6, $format = 'ALL') { switch ($format) { case 'ALL': $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~'; break; case 'CHAR': $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~'; break; case 'NUMBER': $chars = '0123456789'; break; default : $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~'; break; } mt_srand((double) microtime() * 1000000 * getmypid()); $password = ""; while (strlen($password) < $len) { $password.=substr($chars, (mt_rand() % strlen($chars)), 1); } return $password; }</code>
It is recommended not to use mt_srand((double) microtime() * 1000000 * getmypid());
to spread random seeds. Now this function can be completed automatically. In addition, the value you sow is fixed.
There is no problem with the code. I have tested it both on the Alibaba Cloud server and locally. The chance of the same seeds is already very low.
php7 uses random-int