Get a random byte string.

@param int  $byteLength the length of the random string in bytes
@param bool $rawBytes   return the raw random string if true or hex
encoded when false (default)

@return string the random string of specified length
179554
public function getRandom($byteLength = 16, $rawBytes = false)
    {
        $randomBytes = random_bytes($byteLength);

        if ($rawBytes) {
            return $randomBytes;
        }

        return bin2hex($randomBytes);
    }
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);
    }
3707
public function generateKey($length = null) {
		if (!$length) {
			$length = $this->defaultLength;
		}

		if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
			$function = 'random_bytes';
		} elseif (extension_loaded('openssl')) {
			$function = 'openssl_random_pseudo_bytes';
		} else {
			trigger_error('Not secure', E_USER_DEPRECATED);
			return Random::pwd($length);
		}

		$value = bin2hex($function($length / 2));
		if (strlen($value) !== $length) {
			$value = str_pad($value, $length, '0');
		}
		return $value;
	}
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;
    }
89761
def bytes(num, check_result=False):
    

    if num <= 0:
        raise ValueError("'num' should be > 0")
    buf = create_string_buffer(num)
    result = libcrypto.RAND_bytes(buf, num)
    if check_result and result == 0:
        raise RandError("Random Number Generator not seeded sufficiently")
    return buf.raw[:num]
1374538
public static function randomUid($length = 16)
    {
        if(!isset($length) || intval($length) <= 8 ){
            $length = 16;
        }

        if (function_exists('random_bytes')) {
            return bin2hex(random_bytes($length));
        }

        if (function_exists('mcrypt_create_iv')) {
            return bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
        }

        if (function_exists('openssl_random_pseudo_bytes')) {
            return bin2hex(openssl_random_pseudo_bytes($length));
        }
    }
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;
    }
691903
public static function raw($length)
    {
		$data = '';
		while(strlen($data) < $length) {
			$data .= self::getRandomBytes($length);
		}
		return substr($data, 0, $length);
    }
354830
public static function byteStrBetween($string, $start, $end = null)
    {
        if (($startPos = static::byteStrPos($string, $start)) !== false) {
            if ($end) {
                if (($end_pos = static::byteStrPos($string, $end, $startPos + static::byteLength($start))) !== false) {
                    return static::byteSubstr($string, $startPos + static::byteLength($start), $end_pos - ($startPos + static::byteLength($start)));
                }
            } else {
                return static::byteSubstr($string, $startPos);
            }
        }
        return false;
    }
1089749
public String getRandomText(final int minLength, final int maxLength) {
    validateMinMaxParams(minLength, maxLength);

    StringBuilder sb = new StringBuilder(maxLength);
    int length = minLength;
    if (maxLength != minLength) {
      length = length + random.nextInt(maxLength - minLength);
    }
    while (length > 0) {
      if (sb.length() != 0) {
        sb.append(" ");
        length--;
      }
      final double desiredWordLengthNormalDistributed = 1.0 + Math.abs(random.nextGaussian()) * 6;
      int usedWordLength = (int) (Math.min(length, desiredWordLengthNormalDistributed));
      String word = getRandomWord(usedWordLength);
      sb.append(word);
      length = length - word.length();
    }
    return sb.toString();

  }
64400
public function read(int $maxLength) : string
    {
        if ($this->closed) {
            throw new IoException('The stream is closed.');
        }

        try {
            $data = ErrorCatcher::run(function() use ($maxLength) {
                return fread($this->handle, $maxLength);
            });
        } catch (\ErrorException $e) {
            throw new IoException($e->getMessage(), 0, $e);
        }

        if ($data === false) {
            return '';
        }

        return $data;
    }
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;
    }
301789
static function get_random_str($length = 16)
    {
        $str = "";
        $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($str_pol) - 1;
        for ($i = 0; $i < $length; $i++) {
            $str .= $str_pol[mt_rand(0, $max)];
        }
        return $str;
    }
862458
public function generateRandomString($length)
    {
        do {
            $bytes = base64_encode($this->random->nextBytes($length));
            $string = substr(strtr($bytes, array('+' => '', '/' => '', '=' => '')), 0, $length);
        } while (strlen($string) < $length);
        return $string;
    }
248260
public static function generate( $hashLength = self::MAX_HASH_LENGTH, $hashSeed = self::All )
    {
        //	If we ain't got what you're looking for, return simple md5 hash...
        if ( !isset( static::$_hashSeeds, static::$_hashSeeds[$hashSeed] ) || !is_array( static::$_hashSeeds[$hashSeed] ) )
        {
            return md5( time() . mt_rand() . time() );
        }

        //	Randomly pick elements from the array of seeds
        for ( $_i = 0, $_hash = null, $_size = count( static::$_hashSeeds[$hashSeed] ) - 1; $_i < $hashLength; $_i++ )
        {
            $_hash .= static::$_hashSeeds[$hashSeed][mt_rand( 0, $_size )];
        }

        return $_hash;
    }
426665
public static final byte[] generateBytes(final Random rand, final int bytes)
	{
		if (bytes < 1)
			throw new IllegalArgumentException("bytes must be >= 1");

		final byte[] buffer = new byte[bytes];
		rand.nextBytes(buffer);
		return buffer;
	}
999316
public static function random($length = null)
    {
        if ($length === null) {
            $length = Configure::read('Muffin/Tokenize.length', self::DEFAULT_LENGTH);
        }

        return bin2hex(Security::randomBytes($length / 2));
    }