Home > Backend Development > PHP Tutorial > 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
Release: 2016-07-06 13:53:01
Original
1383 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template