Return random string

@param  int $length
@param  int $type

@return string

@throws \InvalidArgumentException
973327
public function random($length = 32, $type = self::RAND_ALL)
    {
        $string = '';

        // todo: make to halper?
        foreach ($this->dictionary as $pos => $item)
        {
            $key = (1 << $pos);
            if ($type >= $key)
            {
                $string .= $item;
                $type -= $key;
            }
        }

        if (empty($string))
        {
            throw new \InvalidArgumentException("Invalid random string type [{$type}].");
        }

        return $this->rand($string, $length);
    }
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);
    }
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;
    }
686260
public static function random(int $length): string
    {
        $byteLength = ($length < 10) ? 10 : $length;
        $chars = base64_encode(random_bytes($byteLength));
        $chars = str_replace(["+", "/", "="], "", $chars);
        return substr($chars, 0, $length);
    }
987168
public static function random($length) {
    if ($length <= 0) return '';
    $str = '';
    for ($i = 0; $i < $length; $i++) {
      $rand = rand(0, 35);
      if ($rand >= 10)
        $str .= chr(ord('a') + $rand-10); // a - z
      else
        $str .= chr(ord('0') + $rand);    // 0 - 9
    }
    
    return $str;
  }
69588
public function random($length = 32, $type = 'alnum')
    {
        switch ($type) {

            case 'alpha':
                $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;

            case 'alnum':
            case 'alphanum':
                $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;

            case 'num':
            case 'numeric':
                $pool = '0123456789';
                break;

            case 'md5':
                return md5(uniqid(mt_rand()));
                break;

            default:
                throw new \Exception("Invalid random string type [{$type}].");
        }

        return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
    }
918178
public static function random($length = 16)
    {
        $string = '';

        while (($len = strlen($string)) < $length) {
            $size = $length - $len;

            $bytes = static::randomBytes($size);

            $string .= substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $size);
        }

        return $string;
    }
131141
public static function random($type = 'alnum', $length = 16)
    {
        switch ($type) {
            case 'normal':
                $key = mt_rand();
                break;
            case 'unique':
                $key = md5(uniqid(mt_rand()));
                break;
            case 'sha1':
                $key = sha1(uniqid(mt_rand(), true));
                break;
            case 'alnum':
                $key = '0123456789'.static::$alpha;
                break;
            case 'alpha':
                $key = static::$alpha;
                break;
        }

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

        return $random;
    }
697481
public static function generate($length = 32, $charset = 'abcdefghijklmnopqrstuvwxyz')
    {
        // Type checks:
        if (!is_numeric($length)) {
            throw new \InvalidArgumentException(
                'random_str - Argument 1 - expected an integer'
            );
        }
        if (!is_string($charset)) {
            throw new \InvalidArgumentException(
                'random_str - Argument 2 - expected a string'
            );
        }

        if ($length < 1) {
            // Just return an empty string. Any value < 1 is meaningless.
            return '';
        }

        // Remove duplicate characters from $charset
        $split = str_split($charset);
        $charset = implode('', array_unique($split));

        // This is the maximum index for all of the characters in the string $charset
        $charset_max = strlen($charset) - 1;
        if ($charset_max < 1) {
            // Avoid letting users do: random_str($int, 'a'); -> 'aaaaa...'
            throw new \LogicException(
                'random_str - Argument 2 - expected a string that contains at least 2 distinct characters'
            );
        }
        // Now that we have good data, this is the meat of our function:
        $random_str = '';
        for ($i = 0; $i < $length; ++$i) {
            $r = random_int(0, $charset_max);
            $random_str .= $charset[$r];
        }

        return $random_str;
    }
267356
protected static function generateRandomString($length)
    {
        $characters = static::CHARACTERS;

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

        return $randomString;
    }
519732
public static function randomString($length)
    {
        $key = '';
        $keys = array_merge(range('a', 'z'));

        for ($i = 0; $i < $length; ++$i) {
            $key .= $keys[array_rand($keys)];
        }

        return $key;
    }
184901
public static function randomString(int $length): string
    {
        try {
            $rand = bin2hex(random_bytes($length));
            // bytes_length = length * 2
            $rand = substr($rand, 0, $length);
        } catch (\Exception $ce) {
            $rand = Str::randomLatinNumeric($length);
        }

        return $rand;
    }
117537
public function generateHardReadableString($length)
    {
        return $this->generateRandomString($length, self::SYMBOLS . self::CHARS . self::NUMBERS . self::SYMBOLS);
    }
28970
public static function random($length = 16)
    {

        if (PHP_MAJOR_VERSION > 7) {
            return static::random7($length);
        }

        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
    }
207821
public static String randomString(int _length) {
        if (_length <= 0) {
            return "";
        }
        Random random = new Random();
        char[] buf = new char[_length];
        for (int idx = 0; idx < buf.length; ++idx)
            buf[idx] = SYMBOLS[random.nextInt(SYMBOLS.length)];
        return new String(buf);
    }
1078518
static function getRandomString($length = 8)
    {

        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

        $value = '';
        $charsLength = strlen($chars) - 1;

        for ($i = 1 ; $i <= $length; $i++) {
            $value .= $chars[rand(0, $charsLength)];
        }

        return $value;

    }
1148518
protected function generate($length = null)
    {
        $generator = new RandomGenerator();
        $result = $generator->randomToken('sha256');
        if ($length !== null) {
            return substr($result, 0, $length);
        }
        return $result;
    }