Generate a random string

@throws \Exception

@return string
976226
protected function generateRandomString()
    {
        if (!extension_loaded('openssl')) {
            throw new \Exception('The OpenSSL PHP extension is required.');
        }

        $length = 32;
        $bytes = openssl_random_pseudo_bytes($length);

        return strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
    }
1288909
protected function generateNumeric()
    {
        if (is_null($this->size) && $this->pattern == static::DEFAULT_PATTERN) {
            return $this->generateInteger();
        }

        return $this->generateString($this->getNumericGenerator());
    }
728272
private function random(int $length = 32): string
    {
        try {
            if (empty($string = random_bytes($length))) {
                throw new \RuntimeException("Unable to generate random string");
            }
        } catch (\Throwable $e) {
            throw new \RuntimeException("Unable to generate random string", $e->getCode(), $e);
        }

        return substr(base64_encode($string), 0, $length);
    }
831860
public function generate() {
		$tmp_pdf_path = create_tmp_file();
		$success = $this->generateWithPrince( null, null, $this->generateHtml(), $tmp_pdf_path );
		if ( true !== $success ) {
			throw new \Exception( 'Failed to create PDF file' );
		}
		$output_path = $this->timestampedFileName( 'jpg' );
		$this->convert( $tmp_pdf_path, $output_path );
		delete_transient( 'dirsize_cache' ); /** @see get_dirsize() */
		return $output_path;
	}
309651
public function generate(): string
    {
        try {
            $token = random_bytes(32);
        } catch(\Throwable $e) {
            throw new InsufficientStrengthException($e->getMessage(), $e->getCode(), $e);
        }

        return $token;
    }
1170252
public static function random2($length = 16)
        {
            if (function_exists('openssl_random_pseudo_bytes')) {
                $bytes = openssl_random_pseudo_bytes($length * 2);

                if ($bytes === false) {
                    throw new Exception('Unable to generate random string.');
                }

                return substr(repl(array('/', '+', '='), '', base64_encode($bytes)), 0, $length);
            }

            return static::quickRandom($length);
        }
1270555
public function generateVerifyCode(): string
    {
        if ($this->minLength > $this->maxLength) {
            $this->maxLength = $this->minLength;
        }
        if ($this->minLength < 3) {
            $this->minLength = 3;
        }
        if ($this->maxLength > 20) {
            $this->maxLength = 20;
        }
        $length = random_int($this->minLength, $this->maxLength);

        $letters = 'bcdfghjklmnpqrstvwxyz';
        $vowels = 'aeiou';
        $code = '';
        for ($i = 0; $i < $length; ++$i) {
            if (($i % 2 && random_int(0, 10) > 2) || (!($i % 2) && random_int(0, 10) > 9)) {
                $code .= $vowels[random_int(0, 4)];
            } else {
                $code .= $letters[random_int(0, 20)];
            }
        }

        return $code;
    }
995263
public static function random_string($length) {
        $key = '';
        $keys = array_merge(range(0, 9), range('a', 'z'));

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

        return $key;
    }
154433
public function generateId()
    {
        return sprintf(
            '%08X-%04X-%04X-%02X%02X-%012X',
            mt_rand(),
            mt_rand(0, 65535),
            bindec(substr_replace(
                sprintf('%016b', mt_rand(0, 65535)), '0100', 11, 4)
            ),
            bindec(substr_replace(sprintf('%08b', mt_rand(0, 255)), '01', 5, 2)),
            mt_rand(0, 255),
            mt_rand()
        );
    }
550216
private function generate(): void
    {
        $this->hexUUID = \sprintf(
            '%s-%s-%s-%s-%s',
            // 8 hex characters
            \bin2hex(\random_bytes(4)),
            // 4 hex characters
            \bin2hex(\random_bytes(2)),
            // "4" for the UUID version + 3 hex characters
            // 0x4000 is 16384 in int
            // 0x4fff is 20479 in int
            \dechex(\random_int(16384, 20479)),
            // (8, 9, a, or b) for the UUID variant 1 plus 3 hex characters
            //  0x8000 is 32768 in int
            //  0xbfff is 49151 in int
            \dechex(\random_int(32768, 49151)),
            // 12 hex characters
            \bin2hex(\random_bytes(6))
        );
    }
1327352
public function random()
    {
        $keys = array_keys($this->elements);

        return $this->elements[$keys[rand(0, sizeof($keys) - 1)]];
    }
1016423
protected function _generateUUID()
    {
        $data = null;
        switch (true) {
            case $this->_isCallable('random_bytes'):
                $data = $this->_randomBytes(16);
                break;
            case $this->_isCallable('openssl_random_pseudo_bytes'):
                $data = $this->_opensslRandomPseudoBytes(16);
                break;
            case $this->_isCallable('mt_rand'):
                $data = $this->_generateBytesWithMtRand(16);
                break;
            default:
                return 'unavailable';
        }

        return $this->_generateUUIDFromData($data);
    }
834989
public function random_string($length = 32)
    {
        return substr(str_shuffle(str_repeat(
            $x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            ceil($length / strlen($x))
        )), 1, $length);
    }
1329373
public function getRandom()
    {
        $bytes = unpack('C7', $this->generator->getBytes(7));
        $lastByte = array_pop($bytes) & 0b00011111;
        $result = 0.0;

        foreach ($bytes as $byte) {
            $result = ($byte + $result) / 256;
        }

        $result = ($lastByte + $result) / 32;

        return $result;
    }
1222178
private function generateSalt()
    {
        $str = '';
        $length = 64;
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(){}[]/|`,.?+-_=:;<> ';
        $count = strlen($chars);
        
        while ($length--) {
            $str .= $chars[mt_rand(0, $count - 1)];
        }
        
        return 'base64:' . base64_encode($str);
    }
1346506
protected static function generateSalt(): string
    {
        $result = '';

        static::validateSaltMinLength();

        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $poolSize = strlen($pool);
        for ($i = 0; $i < static::$saltMinLength; $i++) {
            $result .= $pool[rand(0, $poolSize - 1)];
        }

        return $result;
    }
800306
private function generate()
    {
        $characters = config('promocodes.characters');
        $mask = config('promocodes.mask');
        $promocode = '';
        $random = [];

        for ($i = 1; $i <= $this->length; $i++) {
            $character = $characters[rand(0, strlen($characters) - 1)];
            $random[] = $character;
        }

        shuffle($random);
        $length = count($random);

        $promocode .= $this->getPrefix();

        for ($i = 0; $i < $length; $i++) {
            $mask = preg_replace('/\*/', $random[$i], $mask, 1);
        }

        $promocode .= $mask;
        $promocode .= $this->getSuffix();

        return $promocode;
    }