Method to generate a random key of a given length.

@param   integer $length The length of the key to generate.

@return  string

@since   2.0
968155
private function getRandomKey($length = self::DEFAULT_RANDOM_BYTE_LENGTH)
    {
        $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;
    }
1184451
public static function generateRandomHandle($length = 32)
    {
        do {
            $handle = substr(md5(mt_rand(0, mt_getrandmax())), 0, $length);
        } while (static::getByField(static::$handleField, $handle));

        return $handle;
    }
1030677
public static function generateSafeHash($length=16)
    {
        $factory = new CryptLib\Random\Factory;
        return preg_replace('/[^A-Za-z0-9\-]/', '', $factory->getLowStrengthGenerator()->generateString($length));
    }
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;
    }
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;
    }
1306662
public static function generate( $length = 16 ) {
		if ( function_exists( 'random_bytes' ) ) {
			return bin2hex( random_bytes( $length ) );
		} elseif ( function_exists( 'openssl_random_pseudo_bytes' ) ) {
			return bin2hex( openssl_random_pseudo_bytes( $length ) );
		} else {
			$id = uniqid();
			$hash = '';
			$str_len = strlen( $id );
			$limit = floor( $length * 2 / $str_len );
			for ( $i = 0; $i < $limit; $i++ ) {
				$hash .= $id;
			}
			$remainder = strlen( $id ) / ( $length * 2 );
			if ( $remainder ) {
				$hash .= substr( $id, 0, $remainder );
			}
			return $hash;
		}
	}
1079061
public static function generate($length=128) {

        if ($length < 32) {

            return substr(self::getUid(), 0, $length);

        } else if ($length == 32) {

            return self::getUid();

        }  else {

            $numString = (int)($length/32) + 1;
            $randNum = "";
            for ($i = 0; $i < $numString; $i++) $randNum .= self::getUid();

            return substr($randNum, 0, $length);

        }

    }
127891
public static function genRandomBytes($length = 16)
    {
        if (function_exists('random_bytes')) {
            return random_bytes($length);
        }

        $sslStr = '';

        /*
         * If a secure randomness generator exists use it.
         */
        if (function_exists('openssl_random_pseudo_bytes')) {
            $sslStr = openssl_random_pseudo_bytes($length, $strong);

            if ($strong) {
                return $sslStr;
            }
        }

        /*
         * Collect any entropy available in the system along with a number
         * of time measurements of operating system randomness.
         */
        $bitsPerRound = 2;
        $maxTimeMicro = 400;
        $shaHashLength = 20;
        $randomStr = '';
        $total = $length;

        // Check if we can use /dev/urandom.
        $urandom = false;
        $handle = null;

        if (@is_readable('/dev/urandom')) {
            $handle = @fopen('/dev/urandom', 'rb');

            if ($handle) {
                $urandom = true;
            }
        }

        while ($length > strlen($randomStr)) {
            $bytes = ($total > $shaHashLength) ? $shaHashLength : $total;
            $total -= $bytes;
            /*
             * Collect any entropy available from the PHP system and filesystem.
             * If we have ssl data that isn't strong, we use it once.
             */
            $entropy = mt_rand() . uniqid(mt_rand(), true) . $sslStr;
            $entropy .= implode('', @fstat(fopen(__FILE__, 'r')));
            $entropy .= memory_get_usage();
            $sslStr = '';

            if ($urandom) {
                stream_set_read_buffer($handle, 0);
                $entropy .= @fread($handle, $bytes);
            } else {
                /*
                 * There is no external source of entropy so we repeat calls
                 * to mt_rand until we are assured there's real randomness in
                 * the result.
                 *
                 * Measure the time that the operations will take on average.
                 */
                $samples = 3;
                $duration = 0;

                for ($pass = 0; $pass < $samples; ++$pass) {
                    $microStart = microtime(true) * 1000000;
                    $hash = sha1(mt_rand(), true);

                    for ($count = 0; $count < 50; ++$count) {
                        $hash = sha1($hash, true);
                    }

                    $microEnd = microtime(true) * 1000000;
                    $entropy .= $microStart . $microEnd;

                    if ($microStart >= $microEnd) {
                        $microEnd += 1000000;
                    }

                    $duration += $microEnd - $microStart;
                }

                $duration = $duration / $samples;

                /*
                 * Based on the average time, determine the total rounds so that
                 * the total running time is bounded to a reasonable number.
                 */
                $rounds = (int) (($maxTimeMicro / $duration) * 50);

                /*
                 * Take additional measurements. On average we can expect
                 * at least $bitsPerRound bits of entropy from each measurement.
                 */
                $iter = $bytes * (int) ceil(8 / $bitsPerRound);

                for ($pass = 0; $pass < $iter; ++$pass) {
                    $microStart = microtime(true);
                    $hash = sha1(mt_rand(), true);

                    for ($count = 0; $count < $rounds; ++$count) {
                        $hash = sha1($hash, true);
                    }

                    $entropy .= $microStart . microtime(true);
                }
            }

            $randomStr .= sha1($entropy, true);
        }

        if ($urandom) {
            @fclose($handle);
        }

        return substr($randomStr, 0, $length);
    }
581507
public static function random($length = 16)
    {
        if (PHP_MAJOR_VERSION > 5) {
            return static::_random($length);
        }

        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
    }
466114
protected function generate_key( $length = 64 ) {
		$unique  = md5( microtime() . rand() );
		$prepend = $this->identifier . '_batch_';

		return substr( $prepend . $unique, 0, $length );
	}
107398
protected function generateHash($length)
    {
        $token = '';
        for ($i = 0; $i < $length; ++ $i) {
            $rand = mt_rand(0, 35);
            
            if ($rand < 26) {
                $char = chr(ord('a') + $rand);
            } else {
                $char = chr(ord('0') + $rand - 26);
            }
            
            $token .= $char;
        }
        return $token;
    }
834989
public function random_string($length = 32)
    {
        return substr(str_shuffle(str_repeat(
            $x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            ceil($length / strlen($x))
        )), 1, $length);
    }
85545
public static function generateRandomAlphanumericString($length = 8)
    {
        $string = '';
        for ($i=0; $i < $length; $i++) {
            $char = '';
            switch (rand(0, 2)) {
                //A-Z
                case 0:
                    $char = chr(rand(65, 90));
                    break;
                //a-z
                case 1:
                    $char = chr(rand(97, 122));
                    break;
                //0-9
                case 2:
                    $char = (string) rand(0, 9);
                    break;

            }
            $string .= $char;
        }

        return $string;
    }
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);
    }
1294007
public static function random($length) {
        $res = '';
        for ($i = 0; $i < $length; $i++) {
            $res .= chr(mt_rand(33, 126));
        }
        return $res;
    }
604768
public function getRandomKey($length = self::TOKEN_LENGTH)
    {
        $chars = "A1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P6Q7R8S9T0U1V2W3X4Y5Z6a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6";
        $key = "";

        for ($i = 0; $i < $length; $i++) {
            $key .= $chars{mt_rand(0, strlen($chars) - 1)};
        }

        return $key;
    }
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;
        }