在laravel控制器中不重复的随机字符串生成器

I created this function in my laravel controller.

function incrementalHash($len = 5){
    $charset = "0123456789abcdefghijklmnopqrstuvwxyz";
    $base = strlen($charset);
    $result = '';

    $now = explode(' ', microtime())[1];
    while ($now >= $base){
        $i = $now % $base;
        $result = $charset[$i] . $result;
        $now /= $base;
    }
    return substr($result, -5);
}

then I have a function to insert something in the database. this function uses the above function. but every time I use it I get the same result from above function. I tried composer dump-autoload and the result changes. I wonder what is happening? why this method always returns the same result. how can I use this method and not receive the same result without dumping autoload? here is my controller:

public function add_user_create()
{
    $user = new User;
    $user->user_id = Request()->input('user_id');
    $user->user_name = Request()->input('user_name');
    $user->fcm = Request()->input('fcm');
    $user->email = Request()->input('email');
    $user->token = Request()->input('token');
    $user->profile_pic = Request()->input('profile_pic');
    $user->api_token = str_random(60);
    $user->ref_ID = $this->incrementalHash(4);
    $user->save();
}

I suggest you to use what Laravel provides to generate a random string. like: strtolower(str_random(4)) as mentioned by @kenken9999

However, Here is why I think it gave same result for you:

I executed your function multiple times and these are the outputs:

becpy
becqa
becqd
becqd
becqe

I think when you checked them they just happend to be same and when you did composer dump-autoload you happened to see a different output.

Let me know If I am wrong.

Did you call this function many times during a very short time? Then I believe the issue is microtime(). This function returns a string separated by a space. The first part is the fractional part of seconds, the second part is the integral part.

Thus, if the function is called during the same second, $now should be the same, based on which $result will not change.
Further, if the function is called during a short time (let's say several seconds), $now would be similar (1283846202 and 1283846203 for example). In this case, only the right part of $result would vary.