Home  >  Article  >  Backend Development  >  Why is the result of Alibaba Cloud server’s random number generation fixed?

Why is the result of Alibaba Cloud server’s random number generation fixed?

WBOY
WBOYOriginal
2016-07-06 13:53:011363browse

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>

Reply content:

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.
Why is the result of Alibaba Cloud server’s random number generation fixed?

php7 uses random-int

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn