Generate code

@param integer $length number of characters in generated code

@return string
512932
public function generateCode($length)
    {
        if (isset($_SESSION['captcha_code'])) {
            $code = $_SESSION['captcha_code'];
        } else {
            // list all possible characters, similar looking characters and vowels have been removed
            $possible = '23456789bcdfghjkmnpqrstvwxyz';
            $code = '';
            for ($i=0; $i < $length; $i++) {
                $code .= \substr($possible, \mt_rand(0, \strlen($possible)-1), 1);
            }
            $_SESSION['captcha_code'] = $code;
        }
        return $code;
    }
919915
protected function keygen($length)
    {
        $key = '';
        for($i = 1; $i < $length; $i++) {
            $key .= mt_rand(0,9);
        }
        $key .= mt_rand(1,9);
        
        return $key;
    }
398892
protected function _createId($length = 32)
	{
		$id = '';

		while (strlen($id) < $length)  {
			$id .= mt_rand(0, mt_getrandmax());
		}

		$id	= md5(uniqid($id, true));

		return $id;
	}
1149284
public static function rand($length)
    {
        $chars = '~)!abc}def#ghijkl[m-no.pqrs]tu;v|wx+yzA%BC(D:EF{GHI&JKLM=NOP*QRS?TU4567VWXYZ_012389' .uniqid();
        $str   =  '';
        $size  = strlen($chars);

        for ($i = 0; $i < $length; $i++) {
            $str .= $chars[rand(0, $size - 1)];
        }
        
        return $str;
    }
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);
	}
142290
protected function generateString($length)
    {
        $bytes = ceil($length / 2);
        $string = bin2hex(random_bytes($bytes));

        return substr($string, 0, $length);
    }
1072553
private function generateCode(int $length = 4, string $prefix = '', string $suffix = '')
    {
        for ($i = 0; $i < $length; $i++) {
            $prefix .= random_int(0, 1) ? chr(random_int(65, 90)) : random_int(0, 9);
        }

        return $prefix.$suffix;
    }
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;
    }
1232624
public static function GenerateRandomString(int $length = 20)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
58141
public function getRandomString(int $length = 64): string
    {
        $chars  = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()';
        $random = '';

        for ($i = 0; $i < $length; $i++) {
            $random .= substr($chars, random_int(0, strlen($chars) - 1), 1);
        }

        return $random;
    }
192900
public static function randomString(int $length): string
    {
        $random = '';
        $passes = 0;

        do {
            foreach (str_split(openssl_random_pseudo_bytes($length * 3)) as $c) {
                $ascii = ord($c);

                if ($ascii >= 48 && $ascii <= 57 || $ascii >= 65 && $ascii <= 90 || $ascii >= 97 && $ascii <= 122) {
                    $random .= chr($ascii);
                }
            }
        } while (strlen($random) < $length && $passes++ < 50);

        if ($passes == 50) {
            throw new Exception('Unable to generate random string even after a lot passes... something is really wrong!');
        }

        return substr($random, 0, $length);
    }
1021750
protected function createToken($length = 32)
    {
        static $chars = '0123456789abcdef';
        $max = strlen($chars) - 1;
        $token = '';
        $name = $this->getName();

        for ($i = 0; $i < $length; ++$i) {
            $token .= $chars[(mt_rand(0, $max))];
        }

        return md5($token . $name);
    }
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;
    }
553465
function generateCode($len)
  {
    $code = '';

    for($i = 1, $cslen = strlen($this->charset); $i <= $len; ++$i) {
      $code .= strtoupper( $this->charset{rand(0, $cslen - 1)} );
    }
    return $code;
  }
221482
public function generateSalt($length = 64)
    {
        $chars  = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $chars .= '!@#$%^&*()';
        $chars .= '-_ []{}<>~`+=,.;:/?|';

        $factory = new Factory();
        $generator = $factory->getGenerator(new Strength(Strength::MEDIUM));

        return $generator->generateString($length, $chars);
    }
375011
public static function randAlphabet($length)
    {
        if ($length < 0) $length = 1;
        if ($length > 64) $length = 64;
        $code = [];
        for ($i = 0; $i < $length; $i++) {
            $code[$i] = self::$alphaCodeSet[mt_rand(0, strlen(self::$alphaCodeSet) - 1)];
        }
        return implode("", $code);
    }
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');
    }