Generates a random password
@param int $length Length of the password to generate. Default is 8
@return string
189361
public static function randomPassword($length = 8, $string = null) {
        if (!$string)
            $string = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ123456789&^%$#@!_-+=';
        $chars = str_split(str_shuffle($string));
        $password = '';
        foreach (array_rand($chars, $length) as $key) {
            $password .= $chars[$key];
        }
        return $password;
    }
1236134
private function _getRandomKey($length = 256)
	{
		$key = '';
		$salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$saltLength = strlen($salt);

		// Build the random key.
		for ($i = 0; $i < $length; $i++)
		{
			$key .= $salt[mt_rand(0, $saltLength - 1)];
		}

		return $key;
	}
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);
    }
599538
private static function generateRandomString($length = 10)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $string = '';

        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[rand(0, strlen($characters) - 1)];
        }

        return $string;
    }
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);
	}
491599
protected function keygen($length)
	{
		$hex = !is_bool($this->hex) ?: $this->hex;
		$bytelength = $hex ? ceil($length / 2) : $length;

		if (function_exists('random_bytes')) {
			$bytes = random_bytes($bytelength);
		}

		elseif (function_exists('openssl_random_pseudo_bytes')) {
			$bytes = openssl_random_pseudo_bytes($bytelength);
		}

		elseif (@file_exists('/dev/urandom') && $bytelength < 100) {
			$bytes = file_get_contents('/dev/urandom', false, null, 0, $bytelength);
		}

		else {
			throw new RuntimeException('Cannot generate binary data.');
		}

		return $hex ? substr(bin2hex($bytes), 0, $length) : $bytes;
	}
323351
public static String generatePassword(int length)
  {
    Params.strictPositive(length, "Password length");
    StringBuilder password = new StringBuilder(length);
    Random random = new Random();

    for(int i = 0; i < length; i++) {
      String charCategory = PASSWORD_DICTIONARY[random.nextInt(PASSWORD_DICTIONARY.length)];
      int position = random.nextInt(charCategory.length());
      password.append(charCategory.charAt(position));
    }
    return new String(password);
  }
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);
        }
    }
829385
public function generateRandomString(
        int $length = 10,
        string $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#%^*()-_[]{}<>=/|$'
    ): string {
        $randomStr = '';
        for ($i = 0; $i < $length; $i++) {
            $randomCharIndex = random_int(0, \strlen($chars) - 1);
            $randomStr .= $chars[$randomCharIndex];
        }

        return $randomStr;
    }
924413
public static function generateNonce($length = 30)
    {
        $random = null;
        if ($length < 1) {
            throw new Exception('$length must be greater than 0');
        }

        // Use the new PHP 7 random_bytes call, if available
        if (!$random && function_exists('random_bytes')) {
            $random = random_bytes($length);
        }

        // Get some random binary data from open ssl, if available
        if (!$random && function_exists('openssl_random_pseudo_bytes')) {
            $random = openssl_random_pseudo_bytes($length);
        }

        // Fallback to /dev/urandom
        if (!$random && is_readable('/dev/urandom')) {
            if (($handle = @fopen('/dev/urandom', 'rb')) !== false) {
                $random = @fread($handle, $length);
                @fclose($handle);
            }
        }

        // Fallback if no random bytes were found
        if (!$random) {
            $random = microtime();

            for ($i = 0; $i < 1000; $i += $length) {
                $random = sha1(microtime() . $random);
            }
        }

        // Convert to base64
        $random = base64_encode($random);

        // Replace unsafe chars
        $random = strtr($random, '+/', '-_');
        $random = str_replace('=', '', $random);

        // Truncate the string to specified lengh
        $random = substr($random, 0, $length);

        return $random;
    }
44373
public function randomString($length = 32)
        {
            $string     = "";
            $possible   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_+=";

            for ($i = 0; $i < $length; $i++) {
                $char = $possible[mt_rand(0, strlen($possible)-1)];
                $string .= $char;
            }

            return $string;
        }
1268348
protected function randomKey($length)
    {
        $string = '';
        while (($len = mb_strlen($string)) < $length) {
            $size = $length - $len;
            $bytes = random_bytes($size);
            $string .= mb_substr(str_replace(['\\', '/', '"', '\''], '', base64_encode($bytes)), 0, $size, 'UTF-8');
        }
        return $string;
    }
1361807
public static function _randomPassword ($length = self::DEFAULT_PASSWORD_LENGTH, $charset = self::DEFAULT_PASSWORD_CHARACTERS)
	{
		
		// Set password variable
		
		$password	= '';

		// Set i to the password length
		
		$i = $length;

		// Loop until the password is the desired length
		
		while ($i > 0)
		{
			
			// Choose a random character from the charset
			
			$char = $charset[mt_rand (0, strlen ($charset) -1)];

			// Make sure that the first character of the password is a number or letter
			
			if ($password == '' && !preg_match('/[0-9A-Za-z]/', $char))
			{
				continue;
			}
			
			// Add the character to the password
			
			$password .= $char;

			// Decrement rounds
			
			$i--;

		}

		// Return the password
		
		return $password;

	}
1294007
public static function random($length) {
        $res = '';
        for ($i = 0; $i < $length; $i++) {
            $res .= chr(mt_rand(33, 126));
        }
        return $res;
    }
46097
public static function randomString(int $length): string
    {
        $characters = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789';
        $string = '';
        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $string;
    }
310560
public static function generateRandomPassword($length = 8, $customAlphabet = null)
    {
        $pass = [];
        if (strlen(trim($customAlphabet))) {
            $alphabet = trim($customAlphabet);
        } else {
            $alphabet = 'abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789';
        }

        $alphaLength = strlen($alphabet) - 1;
        for ($i = 0; $i < $length; $i++) {
            $n = rand(0, $alphaLength);
            $pass[] = $alphabet[$n];
        }

        return implode($pass);
    }
834989
public function random_string($length = 32)
    {
        return substr(str_shuffle(str_repeat(
            $x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            ceil($length / strlen($x))
        )), 1, $length);
    }