Generate a random complex password

@param int $length
@param int $availables
@param int[] $tokens
@return string
485583
public function generate($length=10, $availables=self::CHAR_ALPHADIGIT, array $tokens=array(self::CHAR_ALPHA, self::CHAR_DIGIT)) {
		$tokens	= array_pad($tokens, $length, $availables);
		shuffle($tokens);
		$password	= '';
		foreach( $tokens as $token ) {
			$tokenChars = '';
			foreach( $this->primarySets as $flag => $chars ) {
				if( bintest($token, $flag) ) {
					$tokenChars .= $chars;
				}
			}
			$password .= $tokenChars[mt_rand(0, strlen($tokenChars)-1)];
		}
		return $password;
	}
223863
public static function generateRandomNumberString($length = 6)
    {
        $result = '';

        for ($index = 0; $index < $length; $index++) {
            $result .= mt_rand(0, 9);
        }

        return $result;
    }
353360
protected function generate($length = 40)
    {
        try {
            return bin2hex(random_bytes($length / 2));
        } catch (\TypeError $e) {
            // This is okay, so long as `Error` is caught before `Exception`.
            throw new \common_Exception("An unexpected error has occurred while trying to generate a security token", 0, $e);
        } catch (\Error $e) {
            // This is required, if you do not need to do anything just rethrow.
            throw new \common_Exception("An unexpected error has occurred while trying to generate a security token", 0, $e);
        } catch (\Exception $e) {
            throw new \common_Exception("Could not generate a security token. Is our OS secure?", 0, $e);
        }
    }
255413
public function generateAlphaNumToken($length)
    {
        $characters = str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');

        srand((float)microtime() * 1000000);

        $token = '';

        do
        {
            shuffle($characters);
            $token .= $characters[mt_rand(0, (count($characters) - 1))];
        } while (strlen($token) < $length);

        return $token;
    }
76840
private static function make($characters, $length)
    {
        $token = '';

        do {
            $token .= $characters[random_int(0, count($characters) - 1)];
        } while (strlen($token) < $length);

        return $token;
    }
160556
public static function getRandomPassword($length = null, $countCharsNum = null, $countCharsSpecial = null)
    {
        $chars = [
            'alpha'   => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ',
            'special' => '!$%&=?*-:;.,+~@_',
        ];

        if (null === $length) {
            $length = mt_rand(9, 13);
        }

        if (null === $countCharsNum) {
            $countCharsNum = mt_rand(2, 3);
        }

        if (null === $countCharsSpecial) {
            $countCharsSpecial = mt_rand(1, 2);
        }

        $countCharsAlpha = $length - $countCharsNum - $countCharsSpecial;
        if (0 >= $countCharsAlpha) {
            throw new \LogicException(sprintf('%d chars are not enough to include %d numbers and %d special chars', $lengthAlpha, $countCharsNum, $countCharsSpecial));
        }

        $result = [];

        for ($index = 0; $index < $countCharsAlpha; $index++) {
            $result[] = substr($chars['alpha'], mt_rand(0, 45), 1);
        }

        for ($index = 0; $index < $countCharsNum; $index++) {
            $result[] = mt_rand(1, 9);
        }

        for ($index = 0; $index < $countCharsSpecial; $index++) {
            $result[] = substr($chars['special'], mt_rand(0, 15), 1);
        }

        shuffle($result);

        return join('', $result);
    }
215039
public static function getRandomString(int $length): string {
		
		// list of available chars for random string
		$availableChars = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYWZ';
		
		// create a random string
		return substr(str_shuffle($availableChars . $availableChars), 0, $length);
		
	}
1015426
public function generateStringForRanges($length, array $availableCharacterRanges)
    {
        $count = 0;
        foreach ($availableCharacterRanges as $key => $range) {
            $availableCharacterRanges[$key]['offset'] = $count;
            $count += $range['to'] - $range['from'] + 1;
            $availableCharacterRanges[$key]['max'] = $count;
        }
        $result = '';
        for ($i = 0; $i < $length; $i++) {
            $rand = Rand::getInteger(0, $count - 1);
            foreach ($availableCharacterRanges as $range) {
                if ($rand < $range['max']) {
                    $result .= chr($rand - $range['offset'] + $range['from']);
                    break;
                }
            }
        }
        return $result;
    }
109694
public static function random($charLists = null, $length = 32)
    {
        if (!$charLists) {
            $charLists = static::ALNUM;
        }

        $string   = '';
        $poolSize = mb_strlen($charLists) - 1;

        for ($i = 0; $i < $length; $i++) {
            $string .= mb_substr($charLists, mt_rand(0, $poolSize), 1);
        }

        return $string;
    }
813575
static public function generateString($length) {
        $token = '';
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $maxIndex = strlen($chars) - 1;
        
        for ($i = 0; $i < $length; $i++) {
            $token .= $chars[mt_rand(0, $maxIndex)];
        }
         
        return $token;
    }
608699
public static function generateUniqueRandomAlphaNumericString($length){
        if(!function_exists('openssl_random_pseudo_bytes'))
            throw new Exception("openssl_random_pseudo_bytes is not defined!");

        if($length % 2 == 0)
            return bin2hex(openssl_random_pseudo_bytes($length));

        return substr(bin2hex(openssl_random_pseudo_bytes($length + 1)), 0, -1);
    }
649381
public static function generateRandomAlphaNumericString($length) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $size = strlen($chars);

        $randomChars = array();
        for ($i = 0; $i < $length; $i++) {
            $randomChars[] = $chars[mt_rand(0, $size - 1)];
        }

        return implode('', $randomChars);
    }
976860
public static function randomBytes($length = 16)
    {
        if (function_exists('random_bytes')) {
            $bytes = random_bytes($length);
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
            $bytes = openssl_random_pseudo_bytes($length, $strong);
            if (false === $bytes || false === $strong) {
                throw new RuntimeException('Unable to generate random string.');
            }
        } else {
            throw new RuntimeException('OpenSSL extension is required for PHP 5 users.');
        }

        return $bytes;
    }
326181
public static function generateCryptoKey($length)
    {
        // PHP>=7.0
        if (function_exists('random_bytes')) {
            return random_bytes($length);
        }
        
        $buf = openssl_random_pseudo_bytes($length, $secure);

        if ($buf !== false) {
            return $buf;
        }

        throw new \Exception('PRNG failure');
    }
1069751
public static function random($length = 64, $characters = self::ALNUM)
    {
        static $random_generator = null;

        if (null === $random_generator) {
            $random_generator = is_callable('mt_rand') ? 'mt_rand' : 'rand';
        }

        $len = strlen($characters);
        return join(
            '',
            array_map(
                function () use ($characters, $len, $random_generator) {
                    return $characters{$random_generator(0, $len - 1)};
                },
                range(0, $length - 1)
            )
        );
    }
719506
protected function keygen($length)
	{
		$token = '';
		$tokenlength = round($length * 4 / 3);

		for ($i = 0; $i < $tokenlength; ++$i) {
			$token .= chr(rand(32,1024));
		}

		$token = base64_encode(str_shuffle($token));

		return substr($token, 0, $length);
	}
743789
private function randomToken($length = 32)
    {
        if (!isset($length) || intval($length) <= 8) {
            $length = 32;
        }
        if (function_exists('random_bytes')) {
            return random_bytes($length);
        }
        if (function_exists('mcrypt_create_iv')) {
            return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
        }
        if (function_exists('openssl_random_pseudo_bytes')) {
            return openssl_random_pseudo_bytes($length);
        }

        return @crypt(uniqid());
    }